Functions

template_from_string

{{ include(template_from_string("Hello {{ name }}")) }}
{{ include(template_from_string(page.template)) }}

Arguments

  • template: The template

source

{{ source('template.html') }}
{{ source(some_var) }}
{{ source('template.html', ignore_missing = true) }}

The function uses the same template loaders as the ones used to include templates. So, if you are using the filesystem loader, the templates are looked for in the paths defined by it.

Arguments

  • name: The name of the template to read
  • ignore_missing: Whether to ignore missing templates or not

range

{% for i in range(0, 3) %}
    {{ i }},
{% endfor %}

{# outputs 0, 1, 2, 3, #}
{% for i in range(0, 6, 2) %}
    {{ i }},
{% endfor %}

{# outputs 0, 2, 4, 6, #}
{% for i in 0..3 %}
    {{ i }},
{% endfor %}

Arguments

  • low: The first value of the sequence.
  • high: The highest possible value of the sequence.
  • step: The increment between elements of the sequence.

random

The random function returns a random value depending on the supplied parameter type:

  • a random item from a sequence;
  • a random character from a string;
  • a random integer between 0 and the integer parameter (inclusive).
{{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #}
{{ random('ABC') }}                         {# example output: C #}
{{ random() }}                              {# example output: 15386094 (works as the native PHP mt_rand function) #}
{{ random(5) }}                             {# example output: 3 #}

Arguments

  • values: The values

parent

{% extends "base.html" %}

{% block sidebar %}
    <h3>Table Of Contents</h3>
    ...
    {{ parent() }}
{% endblock %}

The parent() call will return the content of the sidebar block as defined in the base.html template.

min

{{ min(1, 3, 2) }}
{{ min([1, 3, 2]) }}
{{ min({2: "e", 3: "a", 1: "b", 5: "d", 4: "c"}) }}
{# returns "a" #}

max

{{ max(1, 3, 2) }}
{{ max([1, 3, 2]) }}
{{ max({2: "e", 1: "a", 3: "b", 5: "d", 4: "c"}) }}
{# returns "e" #}

include

{{ include('template.html') }}
{{ include(some_var) }}

Included templates have access to the variables of the active context.

If you are using the filesystem loader, the templates are looked for in the paths defined by it.

{# template.html will have access to the variables from the current context and the additional ones provided #}
{{ include('template.html', {foo: 'bar'}) }}
{# only the foo variable will be accessible #}
{{ include('template.html', {foo: 'bar'}, with_context = false) }}
{# no variables will be accessible #}
{{ include('template.html', with_context = false) }}
{{ include('sidebar.html', ignore_missing = true) }}
{{ include(['page_detailed.html', 'page.html']) }}

Arguments

  • template: The template to render
  • variables: The variables to pass to the template
  • with_context: Whether to pass the current context variables or not
  • ignore_missing: Whether to ignore missing templates or not
  • sandboxed: Whether to sandbox the template or not

date

{% if date(user.created_at) < date('-2days') %}
    {# do something #}
{% endif %}

The argument must be in one of PHP’s supported date and time formats.

{% if date(user.created_at) < date('-2days', 'Europe/Paris') %}
    {# do something #}
{% endif %}
{% if date(user.created_at) < date() %}
    {# always! #}
{% endif %}

Arguments

  • date: The date
  • timezone: The timezone

cycle

{% set start_year = date() | date('Y') %}
{% set end_year = start_year + 5 %}

{% for year in start_year..end_year %}
    {{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}
{% set fruits = ['apple', 'orange', 'citrus'] %}

{% for i in 0..10 %}
    {{ cycle(fruits, i) }}
{% endfor %}

Arguments

  • position: The cycle position

constant

{{ some_date|date(constant('DATE_W3C')) }}
{{ constant('Namespace\\Classname::CONSTANT_NAME') }}
{{ constant('RSS', date) }}

block

<title>{% block title %}{% endblock %}</title>

<h1>{{ block('title') }}</h1>

{% block body %}{% endblock %}

attribute

{{ attribute(object, method) }}
{{ attribute(object, method, arguments) }}
{{ attribute(array, item) }}
{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}