Standard Twig Functions
Standard functions
attribute()
Access a dynamic attribute of an object:
attribute()
{{ attribute(object, method) }}
{{ attribute(object, method, arguments) }}
{{ attribute(object, 'data-value') }}
block()
Outputs the content of a block:
block()
{{ block('title') }}
{{ block('title', 'template.html.twig') }}
constant()
Access PHP constants:
constant()
{{ constant('PHP_INT_MAX') }}
{{ constant('App\\Entity\\User::STATUS_ACTIVE') }}
{{ constant('STATUS_ACTIVE', user) }}
cycle()
Cyclically returns elements of an array:
cycle()
{% for row in items %}
<tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
<td>{{ row.name }}</td>
</tr>
{% endfor %}
date()
Creates a date object:
date()
{% set now = date() %}
{% set tomorrow = date('+1 day') %}
{% set specific = date('2026-01-15') %}
{{ date('now')|date('d.m.Y') }}
dump()
Outputs debug information about a variable:
dump()
{{ dump(variable) }}
{{ dump(user, config) }}
{{ dump() }}
tip
dump() only works when debug mode is enabled. Nothing is output in production.
enum()
Access PHP enums:
enum()
{% if status == enum('App\\Enum\\Status').Active %}
html_classes()
Generates a CSS class string:
html_classes()
<div class="{{ html_classes('card', {
'card-active': is_active,
'card-highlighted': is_highlighted,
'card-disabled': is_disabled
}) }}">
include()
Includes a template as a function and returns a string:
include()
{{ include('@common/components/icon.html.twig', {name: 'user'}) }}
{{ include('template.html.twig', with_context: false) }}
max() / min()
Maximum or minimum value:
max() / min()
{{ max(1, 5, 3) }}
{# 5 #}
{{ min(prices) }}
{{ max(user.age, 18) }}
parent()
Content of a block from the parent template:
parent()
{% block sidebar %}
{{ parent() }}
<div>Additional content</div>
{% endblock %}
random()
Random value:
random()
{{ random() }}
{{ random(10) }}
{# 0..10 #}
{{ random(['apple', 'banana', 'cherry']) }}
{{ random('hello') }}
range()
Creates an array of numbers:
range()
{% for i in range(0, 10) %}
{{ i }}
{% endfor %}
{% for i in range(0, 100, 5) %}
{{ i }}
{% endfor %}
source()
Returns the source code of a template without rendering:
source()
{{ source('template.html.twig') }}
{{ source('template.html.twig', ignore_missing: true) }}
template_from_string()
Creates a template from a string:
template_from_string()
{% set tmpl = template_from_string('Hello {{ name }}!') %}
{% include tmpl with {name: 'World'} %}