Execute a twig variable inside another twig variable

血红的双手。 提交于 2019-12-12 02:07:37

问题


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



来源:https://stackoverflow.com/questions/12687382/execute-a-twig-variable-inside-another-twig-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!