Twig tags
Met tags kun je logica toevoegen aan je template, zoals for loops en if statements. Hieronder staan de meest gebruikte tags binnen het webshop template uitgelicht. Voor meer tags kun je de Twig documentatie over Tags bekijken.
For loops
1<h1>Members</h1>
2<ul>
3 {% for user in users %}
4 <li>{{ user.username|e }}</li>
5 {% endfor %}
6</ul>
twig docs: https://twig.symfony.com/doc/3.x/tags/for.html
If statements
1{% if product.stock > 10 %}
2 Available
3{% elseif product.stock > 0 %}
4 Only {{ product.stock }} left!
5{% else %}
6 Sold-out!
7{% endif %}
twig docs: https://twig.symfony.com/doc/3.x/tags/if.html
Include
1{% include 'header.html' %}
2 Body
3{% include 'footer.html' %}
twig docs: https://twig.symfony.com/doc/3.x/tags/extends.html
Set
1{% set foo = 'bar' %}
twig docs: https://twig.symfony.com/doc/3.x/tags/set.html
Extends
1{% extends "base.html" %}
2
3{% block title %}Index{% endblock %}
4{% block head %}
5 {{ parent() }}
6 <style type="text/css">
7 .important { color: #336699; }
8 </style>
9{% endblock %}
10{% block content %}
11 <h1>Index</h1>
12 <p class="important">
13 Welcome on my awesome homepage.
14 </p>
15{% endblock %}
twig docs: https://twig.symfony.com/doc/3.x/tags/extends.html
Macro
1{% macro input(name, value, type = "text", size = 20) %}
2 <input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
3{% endmacro %}