我的 Twig 模板的参数被忽略
My Twig template's parameters are ignored
我在自定义应用程序中使用独立的 Twig 实例,但传递给模板的参数(上下文)似乎被忽略了。
执行以下代码会导致此错误:
致命错误:未捕获的异常 'Twig_Error_Runtime' 消息“变量“msg”不存在于 "hello.twig" (...)
test.php
$options = array(
'charset' => 'utf-8',
'debug' => true,
'strict_variables' => true,
);
$env = new Twig_Environment(new \Twig_Loader_Filesystem(getcwd()), $options);
return $env->render("hello.twig", array('msg' => 'World'));
// I also tried this
return $env->loadTemplate("hello.twig")->render(array('msg' => 'World'));
hello.twig
Hello {{ msg }}
注册全局变量($env->addGlobal('msg', 'Everybody'))不再成功。
我是不是错过了什么或者它应该有效吗?
正如 ElefantPhace 指出的那样,在 twig 模板中的 {{ 之后出现了一个不可加密的 space,导致变量名称错误。用常规 space 字符替换它解决了问题:)
我在自定义应用程序中使用独立的 Twig 实例,但传递给模板的参数(上下文)似乎被忽略了。
执行以下代码会导致此错误:
致命错误:未捕获的异常 'Twig_Error_Runtime' 消息“变量“msg”不存在于 "hello.twig" (...)
test.php
$options = array(
'charset' => 'utf-8',
'debug' => true,
'strict_variables' => true,
);
$env = new Twig_Environment(new \Twig_Loader_Filesystem(getcwd()), $options);
return $env->render("hello.twig", array('msg' => 'World'));
// I also tried this
return $env->loadTemplate("hello.twig")->render(array('msg' => 'World'));
hello.twig
Hello {{ msg }}
注册全局变量($env->addGlobal('msg', 'Everybody'))不再成功。
我是不是错过了什么或者它应该有效吗?
正如 ElefantPhace 指出的那样,在 twig 模板中的 {{ 之后出现了一个不可加密的 space,导致变量名称错误。用常规 space 字符替换它解决了问题:)