Tags

Twig tags are control structures that control the flow of the templates. They consist of conditionals (i.e. if/elseif/else), for loops, as well as blocks. Tags appear inside a pair of {%  %} blocks. The closing {%  %} block is "end" followed by the tag name.

As an example, to loop through an array of values you would use the for tag.

<h1>Blog Posts</h1>
{% for post in posts %}
    <p><a href="{{ post.url }}">{{ post.postTitle }}</a></p>
{% endfor %}

You could also use the if tag to test an expression.

{% if posts|length > 1000 %}
    <p>You have a lot of posts</p>
{% endif %}

List of available tags

for

Loops over items in a sequence (array or hash).

{% for post in posts %}
    <h2>{{ post.postTitle }}</h2>
{% endfor %}

Learn more about the for tag

if

Used to test and see if an expression evaluates to a desired value. 

{% if test %}
{% endif %}

Learn more about the if tag

import

Used to import macros into a template.

{% import 'macros/forms' as forms %}

Learn more about the import tag

include

The include tag will include another template within your theme folder and returns the rendered content from that template file into the current template and namespace.

{% include 'snippets/header' %}

Learn more about the include tag

macro

Macros are similar to functions within regular programing languages. They are often used to output reusable HTML code.

{% macro specialHeading(text) %}
<h2 class="specialHeading">{{ text }}</h2>
{% endmacro %}

{% import '_self' as headings %}

{{ headings.specialHeading('Hey there!') %}

Learn more about the macro tag