问题描述
如何从PHP数组动态渲染twig条件
PHP 数据数组
arry('label'=>'test','parentRoleExp'=>"is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG')"
树枝代码
{% set parentRoleExp = '' %}
{% if link['parentRoleExp'] is defined %}
{% set parentRoleExp = link['parentRoleExp'] %}
{% endif %}
{% if parentRoleExp %}
<h1>Admin Blog</h1>
{% else %}
<h1>Blog <?h1>
{% endif %}
以上代码无效
预期结果
{% if is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG') %}
<h1>Admin Blog</h1>
{% else %}
<h1>Blog <?h1>
{% endif %}
我尝试了各种方法但没有奏效。请帮忙
提前致谢
解决方法
这是一个非常糟糕的主意 - 漏洞
但是如果你想...注册 eval() 函数:
$twig = new Twig\Environment($loader);
$twig->addFunction(new Twig\TwigFunction('phpEval','eval'));
在模板中
{% if phpEval(parentRoleExp) %}