在Symfony 3.2(使用twig)中,我正在尝试从数组中动态生成菜单项。
数组:
{% set links = {
dashboard: { path: 'dashboard', title: 'Home' },
page1: { path: 'page1', title: 'Page1' },
page2: { path: 'page2', title: 'Page1' },
}
%}
菜单项循环:
{% for link in links %}
<li>
<a {% if app.request.attributes.get('_route') == link.path %}
class="active"
{% endif %}
href="{{ path('{{link.path}}') }}">{{ link.title }}
</a>
</li>
{% endfor %}
我收到错误An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "{{link.path}}" as such route does not exist.")
。
使用路径({{link.path}}),我收到错误A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in
答案 0 :(得分:4)
{{ path
已经{{
在twig中,<?php echo <?php echo "oops"
等于php函数echo
就像拥有href="{{ path(link.path) }}">{{ link.title }}
I18n::locale('da_DK');
答案 1 :(得分:4)
href="{{ path('{{link.path}}') }}">{{ link.title }}
href="{{ path(link.path) }}">{{ link.title }}
答案 2 :(得分:0)
您的树枝包含一些错误,请尝试以下
{% set links = [
{ "path": 'dashboard', "title": 'Home' },
{ "path": 'page1', "title": 'Page1' }
] %}
{% for link in links %}
<li>
<a
href="{{ path(link.path) }}">{{ link.title }}
</a>
</li>
{% endfor %}