用什么代替 Twig_Loader_String [英] What to use instead of Twig_Loader_String

查看:30
本文介绍了用什么代替 Twig_Loader_String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 Twig_Loader_String 类已被弃用,并将在 Twig 2.0 中删除.此外,源代码中的注释表明它应该永远不要使用".

I see that the Twig_Loader_String class has been deprecated and will be removed in Twig 2.0. Also, the comments in the source indicate that it should "NEVER be used".

包含 Twig 模板的字符串有许多有效的用例.

There are numerous valid use cases for a string containing a Twig template.

问题是:用什么代替?

推荐答案

$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_Loader_String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆