Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Как брать шаблон не из файла, а из переменной? #2

Open
KarelWintersky opened this issue Jan 16, 2025 · 0 comments

Comments

@KarelWintersky
Copy link
Contributor

https://smarty-php.github.io/smarty/stable/api/resources/

        $s = new \Smarty();
        $s->assign("settings", $settings);
        return $s->fetch('string:' . $template);

То есть нужна функция, аналогичная setTemplate(), только

  • setTemplateFile() - файл шаблона
  • setTemplate() - шаблон из строки или setTemplateContent()

Это обратно-несовместие изменение.


Smarty can render templates from a string by using the string: or eval: resource.

The string: resource behaves much the same as a template file. The template source is compiled from a string and stores the compiled template code for later reuse. Each unique template string will create a new compiled template file. If your template strings are accessed frequently, this is a good choice. If you have frequently changing template strings (or strings with low reuse value), the eval: resource may be a better choice, as it doesn\'t save compiled templates to disk.

The eval: resource evaluates the template source every time a page is rendered. This is a good choice for strings with low reuse value. If the same string is accessed frequently, the string: resource may be a better choice.

Adding your own resource type

<?php
class HelloWorldResource extends Smarty\Resource\CustomPlugin {

    protected function fetch($name, &$source, &$mtime) {
        $source = '{$x="hello world"}{$x}'; // load your template here based on $name
        $mtime = time();
    }

}

// ..

$smarty->registerResource('helloworld', new HelloWorldResource());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant