Skip to content

Twig tags

Tags let you add logic to your template, such as for loops and if statements. The most commonly used tags within the webshop template are highlighted below. For additional tags, see the Twig documentation on Tags.

Want to use tags that are not listed below? Try them first in a template that is not connected to a live website. Some tags are not available in our environment, such as the Sandbox tag, or were added in later versions of Twig than the one we use.

For loops

Twig
<h1>Members</h1>
<ul>
    {% for user in users %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
</ul>

twig docs: https://twig.symfony.com/doc/3.x/tags/for.html

If statements

Twig
{% if product.stock > 10 %}
   Available
{% elseif product.stock > 0 %}
   Only {{ product.stock }} left!
{% else %}
   Sold-out!
{% endif %}

twig docs: https://twig.symfony.com/doc/3.x/tags/if.html

Include

Twig
{% include 'header.html' %}
    Body
{% include 'footer.html' %}

twig docs: https://twig.symfony.com/doc/3.x/tags/extends.html

Set

Twig
{% set foo = 'bar' %}

twig docs: https://twig.symfony.com/doc/3.x/tags/set.html

Extends

Twig
{% extends "base.html" %}

{% block title %}Index{% endblock %}
{% block head %}
    {{ parent() }}
    <style type="text/css">
        .important { color: #336699; }
    </style>
{% endblock %}
{% block content %}
    <h1>Index</h1>
    <p class="important">
        Welcome on my awesome homepage.
    </p>
{% endblock %}

twig docs: https://twig.symfony.com/doc/3.x/tags/extends.html

Macro

Twig
{% macro input(name, value, type = "text", size = 20) %}
    <input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
{% endmacro %}

twig docs: https://twig.symfony.com/doc/3.x/tags/macro.html