【发布时间】:2015-09-11 13:47:13
【问题描述】:
是否可以通过调用 Twig_SimpleFunction 来修改当前的 twig 上下文?
我注册了以下函数:
<?php
namespace Craft;
class TwiggedTwigExtension extends \Twig_Extension
{
public function getName()
{
return 'Twigged';
}
public function getFunctions()
{
return array(
'setContextVar' => new \Twig_SimpleFunction('setContextVar', array($this, 'setContextVar'), array('needs_context' => true)),
);
}
public function setContextVar($context, $str, $val)
{
$context['context'][$str] = $val;
var_dump(array_keys($context['context']));
}
}
当从像{{ setContextVar('hellow', 'world') }} 这样的模板调用时,var_dump 会显示修改后的上下文。但是像{{ dump(_context|keys) }} 这样快速检查模板不会显示修改后的上下文。
我是不是走错路了?
【问题讨论】:
-
您是否按照here 的描述正确定义了带有
need_context参数的函数? -
是的,我相信是的。这是一个 pastebin - pastebin.com/D8ZnKKWm
标签: php symfony templates twig