Avadh Patel
Avadh Patel

Reputation: 36

How to render twig condition dynamically from php array

How to render twig condition dynamically from php array

Php data array

arry('label'=>'test','parentRoleExp'=>"is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG')"

twig code

{% set parentRoleExp = '' %}
 {% if link['parentRoleExp'] is defined %}
    {% set parentRoleExp = link['parentRoleExp'] %}
 {% endif %}
{% if parentRoleExp %}
  <h1>Admin Blog</h1>
{% else %}
  <h1>Blog <?h1>
{% endif %}

Above code not working

Expected result

{% if is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG') %}
  <h1>Admin Blog</h1>
{% else %}
  <h1>Blog <?h1>
{% endif %}

I tried with various method but not working. please help

Thanks in advance

Upvotes: 0

Views: 66

Answers (1)

WinterSilence
WinterSilence

Reputation: 409

it's very bad idea - ulnerability

But if you want... register eval() function:

$twig = new Twig\Environment($loader);
$twig->addFunction(new Twig\TwigFunction('phpEval', 'eval'));

in template

{% if phpEval(parentRoleExp) %}

Upvotes: 1

Related Questions