Execute a twig variable inside another twig variab

2019-08-31 03:28发布

问题:

I'm working on a symfony2 project. In a file html.twig i have the following code: {{ app.session.get('menu') | raw }} that refers to a field in a database table contained in a session. The value of this field is :

<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Menu1</a>
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="{{ path('graphe') | raw}}">Graphe</a></li>
</ul>
</div>
</div>

Now at this point the menu is displayed perfectly, but when i click on "Graphe", i get the error No route found for GET /{{ path('graphe') | raw}} which means that the code is not executed but taken as a string instead. Is there any other way it can be executed. Thank you in advance.

回答1:

what you need is an eval like function because for now, twig interpret variable as simple text.

to summarize, you need to write your own filter to interpret variable.

Full answer here : How to call a twig filter dynamically



回答2:

In your twig template embed a controller which renders your user menu. For more info see:

http://symfony.com/doc/current/book/templating.html#embedding-controllers