Skip to main content

All Tags

Reference of all Twig 3.x tags.

Tags​

apply​

Applies filters to a block of content:

apply
{% apply upper %}
This text will become uppercase
{% endapply %}

{% apply spaceless|escape %}
<div> <p>text</p> </div>
{% endapply %}

autoescape​

Controls automatic output escaping:

autoescape
{% autoescape 'html' %}
{{ user_input }}
{% endautoescape %}

{% autoescape false %}
{{ trusted_html }}
{% endautoescape %}

block​

Defines a block that can be overridden in a child template:

block
{% block title %}
Default title
{% endblock %}

{# Short form #}
{% block title 'Default title' %}

{# Output block by name #}
{{ block('title') }}

cache​

Caches a template fragment:

cache
{% cache 'sidebar' ttl(300) %}
{# This block is cached for 5 minutes #}
{% for item in expensive_query %}
{{ item.name }}
{% endfor %}
{% endcache %}

deprecated​

Marks a block or template as deprecated:

deprecated
{% deprecated 'Template "old.html.twig" is deprecated, use "new.html.twig"' %}

{# With package and version specified #}
{% deprecated 'Block "sidebar" is deprecated' package='my/package' since='2.0' %}

do​

Executes an expression without outputting the result:

do
{% do items.append('new') %}
{% do form.setData(data) %}

embed​

Includes a template with the ability to override its blocks:

embed
{% embed '@common/components/card.html.twig' %}
{% block header %}My header{% endblock %}
{% block body %}My content{% endblock %}
{% endembed %}

{# With variables #}
{% embed '@common/components/card.html.twig' with {class: 'primary'} %}
{% block body %}Content{% endblock %}
{% endembed %}

extends​

Inherits from a parent template:

extends
{% extends '@site/layout.html.twig' %}

{# Dynamic inheritance #}
{% extends ajax ? '@site/layout_ajax.html.twig' : '@site/layout.html.twig' %}
tip

extends must be the first tag in the template.

flush​

Forces the output buffer to be sent to the client:

flush
{% flush %}

for​

Iterates over a collection:

for
{% for user in users %}
{{ user.name }}
{% else %}
The list is empty.
{% endfor %}

{# With keys #}
{% for key, value in items %}
{{ key }}: {{ value }}
{% endfor %}

{# With filtering #}
{% for user in users if user.active %}
{{ user.name }}
{% endfor %}

from​

Imports specific macros from a template:

from
{% from '@common/macros/forms.html.twig' import input, select as form_select %}

{{ input('name', '') }}
{{ form_select('role', roles) }}

guard​

Guard block -- content is displayed only when the condition is met:

guard
{% guard mw.user is not null %}
<p>Welcome, {{ mw.user.name }}!</p>
{% endguard %}

if​

Conditional execution:

if
{% if product.stock > 0 %}
In stock
{% elseif product.preorder %}
Pre-order
{% else %}
Out of stock
{% endif %}

import​

Imports macros from a template into a variable:

import
{% import '@common/macros/forms.html.twig' as forms %}

{{ forms.input('email', user.email) }}
{{ forms.textarea('bio', user.bio) }}

include​

Includes another template:

include
{% include '@site/partials/header.html.twig' %}

{# With variables #}
{% include '@common/components/alert.html.twig' with {
type: 'success',
message: 'Saved!'
} %}

{# Without parent context #}
{% include 'template.html.twig' with {var: 'value'} only %}

{# Ignore if not found #}
{% include 'optional.html.twig' ignore missing %}

macro​

Defines a reusable macro:

macro
{% macro button(label, type, attrs) %}
{% set type = type|default('button') %}
<button type="{{ type }}"
{% for key, val in attrs|default({}) %}
{{ key }}="{{ val }}"
{% endfor %}
>{{ label }}</button>
{% endmacro %}

sandbox​

Executes an included template in a sandbox with a restricted set of tags, filters, and functions:

sandbox
{% sandbox %}
{% include 'user_template.html.twig' %}
{% endsandbox %}

set​

Assigns a value to a variable:

set
{% set name = 'World' %}
{% set items = [1, 2, 3] %}
{% set config = {debug: true} %}

{# Block assignment #}
{% set content %}
<p>Multiline content</p>
{% endset %}

types​

Declares variable types for static analysis:

types
{% types {
users: 'array',
title: 'string',
page: 'int'
} %}

use​

Horizontal reuse of blocks (traits):

use
{% use 'blocks/sidebar.html.twig' %}
{% use 'blocks/forms.html.twig' with input as form_input %}

verbatim​

Outputs content without Twig processing:

verbatim
{% verbatim %}
{{ this will not be processed by Twig }}
{% no processing here either %}
{% endverbatim %}

with​

Creates a new scope:

with
{% with %}
{% set temp = compute() %}
{{ temp }}
{% endwith %}

{% with {name: user.name} only %}
Hello, {{ name }}
{% endwith %}