Tags
In Twig, tags are used to tell templates what to do. Tags are used in between {%...%}.
Different types of tags are available such as 'control flow' tags which determine whether something should be executed based on certain conditions, or iteration tags which run a block of code repeatedly.
Here is a list of tags available and an explanation for each.
blocks
Block tags are used for inheritance and act as placeholders and replacements at the same time.
Block names should consist of alphanumeric characters, and underscores. Dashes are not permitted.
do
The do tag works exactly like a regular variable expression {{...}} but doesn't print anything in the output.
{% do 1 + 2 %}
Read more at https://twig.symfony.com/doc/3.x/tags/do.html
for
The for tag loops over each item in a sequence. For example, to display a list of products provided in a variable called products
{% for product in products %} {{ product.title }} {% endfor %}
Read more at https://twig.symfony.com/doc/3.x/tags/for.html
if
The if tag can be used to test if an expression evaluates to true.
{% if product.two_for_one %} This product is on special offer, buy two for one {% endif %}
Read more at
https://twig.symfony.com/doc/3.x/tags/if.html
import
You can put code that is used often into macros
Macros can be in different templates and imported using the import tag.
Read more at https://twig.symfony.com/doc/3.x/tags/import.html
include
The include statement includes a template and returns the rendered content of that file into the current namespace:
{% include 'header.twig' %} body {% include 'footer.twig' %}
Read more at https://twig.symfony.com/doc/3.x/tags/include.html
macros
Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself.
Read more at https://twig.symfony.com/doc/3.x/tags/macro.html