Skip to content

Instantly share code, notes, and snippets.

@alexandresalome
Created September 22, 2013 10:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandresalome/6658708 to your computer and use it in GitHub Desktop.
Save alexandresalome/6658708 to your computer and use it in GitHub Desktop.
Reduce amount of logic in your templating: set business in your array definition, not in template body.
<ul class="nav navbar-nav">
{% set navbar_active = navbar_active|default(null) %}
{% set navbar_menu = navbar_menu|default([
{
'show': app.user and is_granted('ROLE_ADMIN'),
'active': navbar_active == 'design',
'label': 'Design',
'href': path('admin_design'),
},
{
'show': app.user and is_granted('ROLE_ADMIN'),
'active': navbar_active == 'administration',
'label': 'Administration',
'href': path('adminuser_list')
},
]) %}
{% for menu in navbar_menu %}
{% if menu.show|default(true) %}
<li{% if menu.active|default(false) %} class="active"{% endif %}>
<a href="{{ menu.href }}">{{ menu.label }}</a>
</li>
{% endif %}
{% endfor %}
</ul>
@stof
Copy link

stof commented Sep 22, 2013

menu.show|default(true) will not do what you expect, because a false value will give you the default, so the item will always be shown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment