Love the simplicity of Twig; mainly that it allows us to do away with the conditionals that tend to litter most interface code. You know the drill: If this then show that otherwise show the other thing except in such-and-such a case.. Confusing and brittle. Twig’s hierarchical layout is the way to go.
Falling back to a parent block depending on complex output can be tricky however. Here’s a simple way to do it without a bunch of ifs:
1 2 3 4 5 6 7 8 9 10 11 12 |
{% block header_details %} {% set details %} {# Use Ornicar's lovely GravatarBundle to show all Gravatars #} {% for email in user.emails if gravatar_exists(email) %} <img title="{{ email }}" src="{{ gravatar(email, 50) }}" /> {% endfor %} {# Text tagline (may be blank) #} {{ user.tagline }} {% endset %} {# Show generic icon if no details #} {{ details|trim|default(parent())|raw }} {% endblock %} |
Yay. If less.
Or with one ternary conditional if running parent()
through default()
is unclear:
{{ details|trim|raw ?: parent() }}