【发布时间】:2015-06-26 20:47:52
【问题描述】:
我看到 Twig_Loader_String 类已被弃用,并将在 Twig 2.0 中删除。此外,源代码中的 cmets 表明它应该“永远不要使用”。
包含 Twig 模板的字符串有许多有效用例。
问题是:改用什么?
【问题讨论】:
我看到 Twig_Loader_String 类已被弃用,并将在 Twig 2.0 中删除。此外,源代码中的 cmets 表明它应该“永远不要使用”。
包含 Twig 模板的字符串有许多有效用例。
问题是:改用什么?
【问题讨论】:
应该使用Twig_Environment#createTemplate,如issue deprecating Twig_Loader_String:
// the loader is not important, you can even just
// use the twig service in Symfony here
$twig = new \Twig_Environment(...);
$template = $twig->createTemplate('Hello {{ name }}!');
echo $template->render(['name' => 'Bob']);
此代码是最简单的方法,并且绕过了完整的缓存系统。这意味着它没有Twig_Loader_String 的缺点(它不会在您每次调用render 时创建一个新的缓存条目;它在引用其他模板时没有问题;等等),但是它仍然不如使用Twig_Loader_Array(如@AlainTiemblo 的回答所示)或Twig_Loader_Filesystem 那样快。
【讨论】:
Twig_Loader_Array 加载器将 $templateName => $templateContents 数组作为参数,因此可以使用模板名称完成一些缓存内容。
所以这个实现有效:
$templates = array('hello' => 'Hello, {{ name }}');
$env = new \Twig_Environment(new \Twig_Loader_Array($templates));
echo $env->render('hello', array('name' => 'Bob'));
或者:
$env = new \Twig_Environment(new \Twig_Loader_Array(array()));
$template = $env->createTemplate('Hello, {{ name }}');
echo $template->render(array('name' => 'Bob'));
为了澄清谣言,从第一个 Twig 版本开始,Twig_Loader_Array 在其构造函数中采用了一个数组。没有数组初始化Twig_Loader_Array的所有答案都是错误的。
【讨论】:
$tplName = uniqid( 'string_template_', true );
$env = clone $this->getTwig();
$env->setCache(false);
$env->setLoader( new \Twig_Loader_Array( [ $tplName => 'Hello, {{ name }}' ] ));
$html = new Response( $env->render( $tplName, [ 'name' => 'Bob' ] ));
echo $html; // Hello, Bob
【讨论】:
$twig 来自哪里?这不应该是$env->setCache(false) 吗?
createTemplate 比克隆 twig 服务安全得多
试试看
$template = $this->container->get('twig')->createTemplate('hello {{ name }}');
echo $template->render(array('name' => 'Fabien'));
【讨论】:
$environment = new \Twig_Environment(new \Twig_Loader_Array(array()));
$template = $environment->createTemplate('{{ template }} {{ replacements }}');
echo $template->render([replacements]);
【讨论】:
lib/Twig/Loader/Array.php:public function __construct(array $templates)
最好的是: http://twig.sensiolabs.org/doc/2.x/recipes.html#loading-a-template-from-a-string
作为我使用的示例:
public function parse($content, $maxLoops = 3, $context = array())
{
if (strlen($content) < 1) {
return null;
}
for ($i = 0; $i < $maxLoops; $i++) {
$template = $this->container->get('twig')->createTemplate($content);
$result = $template->render( $context );
if ($result == $content) {
break;
} else {
$content = $result;
}
}
return $content;
}
【讨论】:
这实际上似乎按预期工作:
$tplName = uniqid( 'string_template_', true );
$env = clone $this->getTwig();
$env->setLoader( new \Twig_Loader_Array( [ $tplName => 'Hello, {{ name }}' ] ));
$html = new Response( $env->render( $tplName, [ 'name' => 'Bob' ] ));
$cacheName = $env->getCacheFilename( $tplName );
if( is_file( $cacheName ) )
{
unlink( $cacheName );
}
echo $html; // Hello, Bob
我在这里找到了提示:http://twig.sensiolabs.org/doc/recipes.html#using-different-template-sources。
请注意,如果模板字符串来自数据库或类似的东西,则不希望删除缓存文件。我使用此功能来渲染动态创建且寿命很短的模板,通常在调试和测试时。
【讨论】: