在Symfony2中,默认情况下禁用了一些Twig模块.其中一个是调试扩展,它添加了{% debug %}标记(在开发环境中很有用).
要启用它,没有什么困难,您可以将此服务添加到您的配置中:
debug.twig.extension:
class: Twig_Extensions_Extension_Debug
tags:
- { name: 'twig.extension' }
Run Code Online (Sandbox Code Playgroud)
但是如何启用{% sandbox %}标签?
我的问题是扩展的构造函数采用安全策略:
public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
{
$this->policy = $policy;
$this->sandboxedGlobally = $sandboxed;
}
Run Code Online (Sandbox Code Playgroud)
通过阅读twig文档,我看到了本机的方式(没有Symfony2):
$tags = array('if');
$filters = array('upper');
$methods = array(
'Article' => array('getTitle', 'getBody'),
);
$properties = array(
'Article' => array('title', 'body'),
);
$functions = array('range');
$policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);
$sandbox = new Twig_Extension_Sandbox($policy);
$twig->addExtension($sandbox);
Run Code Online (Sandbox Code Playgroud)
在使用沙盒之前,我可以在服务中做类似的事情,但这并不像我们习惯的依赖注入那么清晰.
是否有更好/正确的方法在Symfony2中启用twig的沙箱扩展?