Twig – A modern
template engine
for PHP
A complete guidance with understandable examples and output
Imam Hussain – 4 years of experience in web development
, Twig – A modern template engine for PHP
The Twig code could be variables or expressions, which get replaced
with values when the template is rendered, and tags, which control the
logic of the template.
There are two kinds of tags in Twig:
• Logic tags: {% ... %}
• Output tags: {{ .... }}
Comments
To comment out one or more lines of code (or part of a line) use the
the {# #} syntax.
{# This will be a comment #}
<p>You can also comment out {# part of a line #}.</p>
{# This will be a
multi-line
comment.
#}
Setting Variables
{% set foo1 = 'foo' %}
{% set foo2 = [1, 2] %}
{% set foo3 = {'foo': 'bar'} %}
{{foo1}}
{{foo2[1]}}
{{foo3.foo}}
O/p: foo
2
bar
Filters
Variables can be modified by filters. Filters are separated from the
variable by a pipe symbol (|). Multiple filters can be chained.
{% apply upper %}
This text becomes uppercase
{% endapply %}
abs
The abs filter returns the absolute value.
{% set number = -10 %}
2
, Twig – A modern template engine for PHP
{{ number|abs }}
O/p : 10
batch
The batch filter "batches" items by returning a list of lists with the
given number of items. A second parameter can be provided and used
to fill in missing items:
{% set items = ['a', 'b', 'c'] %}
<table>
{% for row in items|batch(2, 'No item') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
O/p: <table>
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>c</td>
<td>No item</td>
</tr>
</table>
capitalize
The capitalize filter capitalizes a value. The first character will be
uppercase, all others lowercase:
{{ 'my first car'|capitalize }}
O/p : My first car
column
The column filter returns the values from a single column in the input
array.
{% set items = [{ 'fruit' : 'apple'}, {'fruit' : 'orange' }] %}
{% set fruits = items|column('fruit') %}
{{fruits[1]}}
3
, Twig – A modern template engine for PHP
O/p : orange
convert_encoding
The convert_encoding filter converts a string from
one encoding to another. The first argument is the
expected output charset and the second one is the
input charset:{{ “data” | convert_encoding('UTF-
8', 'iso-2022-jp') }} O/p : data
country_name
The country_name filter returns the country name given its ISO-3166
two-letter code:
{{ 'FR'|country_name }} O/p : France
currency_name
The currency_name filter returns the currency
name given its three-letter code:
{{ 'EUR'|currency_name }} O/p : Euro
currency_symbol
The currency_symbol filter returns the currency
symbol given its three-letter code:
{{{ 'JPY'|currency_symbol }} O/p : ¥
data_uri
The data_uri filter generates a URL using the data scheme
date
The date filter formats a date to a given format:
{{ "now"|date("d/m/Y") }} or {{ "now"|date(“d/m/Y”,
"Europe/Paris") }} O/p : 19/07/2022 or 19/07/2022
date_modify
The date_modify filter modifies a date with a given
modifier string:
{{ "now"|date_modify("+1 day")|date("d/m/Y") }}
O/p : 20/07/2022
4
template engine
for PHP
A complete guidance with understandable examples and output
Imam Hussain – 4 years of experience in web development
, Twig – A modern template engine for PHP
The Twig code could be variables or expressions, which get replaced
with values when the template is rendered, and tags, which control the
logic of the template.
There are two kinds of tags in Twig:
• Logic tags: {% ... %}
• Output tags: {{ .... }}
Comments
To comment out one or more lines of code (or part of a line) use the
the {# #} syntax.
{# This will be a comment #}
<p>You can also comment out {# part of a line #}.</p>
{# This will be a
multi-line
comment.
#}
Setting Variables
{% set foo1 = 'foo' %}
{% set foo2 = [1, 2] %}
{% set foo3 = {'foo': 'bar'} %}
{{foo1}}
{{foo2[1]}}
{{foo3.foo}}
O/p: foo
2
bar
Filters
Variables can be modified by filters. Filters are separated from the
variable by a pipe symbol (|). Multiple filters can be chained.
{% apply upper %}
This text becomes uppercase
{% endapply %}
abs
The abs filter returns the absolute value.
{% set number = -10 %}
2
, Twig – A modern template engine for PHP
{{ number|abs }}
O/p : 10
batch
The batch filter "batches" items by returning a list of lists with the
given number of items. A second parameter can be provided and used
to fill in missing items:
{% set items = ['a', 'b', 'c'] %}
<table>
{% for row in items|batch(2, 'No item') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
O/p: <table>
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>c</td>
<td>No item</td>
</tr>
</table>
capitalize
The capitalize filter capitalizes a value. The first character will be
uppercase, all others lowercase:
{{ 'my first car'|capitalize }}
O/p : My first car
column
The column filter returns the values from a single column in the input
array.
{% set items = [{ 'fruit' : 'apple'}, {'fruit' : 'orange' }] %}
{% set fruits = items|column('fruit') %}
{{fruits[1]}}
3
, Twig – A modern template engine for PHP
O/p : orange
convert_encoding
The convert_encoding filter converts a string from
one encoding to another. The first argument is the
expected output charset and the second one is the
input charset:{{ “data” | convert_encoding('UTF-
8', 'iso-2022-jp') }} O/p : data
country_name
The country_name filter returns the country name given its ISO-3166
two-letter code:
{{ 'FR'|country_name }} O/p : France
currency_name
The currency_name filter returns the currency
name given its three-letter code:
{{ 'EUR'|currency_name }} O/p : Euro
currency_symbol
The currency_symbol filter returns the currency
symbol given its three-letter code:
{{{ 'JPY'|currency_symbol }} O/p : ¥
data_uri
The data_uri filter generates a URL using the data scheme
date
The date filter formats a date to a given format:
{{ "now"|date("d/m/Y") }} or {{ "now"|date(“d/m/Y”,
"Europe/Paris") }} O/p : 19/07/2022 or 19/07/2022
date_modify
The date_modify filter modifies a date with a given
modifier string:
{{ "now"|date_modify("+1 day")|date("d/m/Y") }}
O/p : 20/07/2022
4