What's on this page
Filters
Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (|). Multiple filters can be chained. The output of one filter is applied to the next.
{{ page.title|truncate(25)|capitalize }}
filter_by
Filters a pages collection by variable name/value.
{{ collection|filter_by(variable, value) }}
Only regular pages (type page, not redirections) are returned.
Example:
{{ pages|filter_by('section', 'blog') }}
filter
For more complex cases, you should use Twig’s native filter.
Example:
{% set filtered = pages|filter(p => p.generated == false and p.id not in ['page-1', 'page-2']) %}
markdown_to_html
Converts a Markdown string to HTML.
{{ markdown|markdown_to_html }}
{% apply markdown_to_html %}
{# Markdown here #}
{% endapply %}
Examples:
{% set markdown = '**This is bold text**' %}
{{ markdown|markdown_to_html }}
{% apply markdown_to_html %}
**This is bold text**
{% endapply %}
toc
Extracts only headings matching the given selectors (h2, h3, etc.), or those defined in config pages.body.toc if not specified.
The format parameter defines the output format: html (default) or json.
The url parameter is used to build links to headings.
{{ markdown|toc(format, selectors, url) }}
Examples:
{{ page.body|toc }}
{{ page.body|toc('html') }}
{{ page.body|toc(selectors=['h2']) }}
{{ page.body|toc(url=url(page)) }}
json_decode
Converts a JSON string to an array.
{{ json|json_decode }}
Example:
{% set json = '{"foo": "bar"}' %}
{% set array = json|json_decode %}
{{ array.foo }}
yaml_parse
Converts a YAML string to an array.
{{ yaml|yaml_parse }}
Example:
{% set yaml = 'key: value' %}
{% set array = yaml|yaml_parse %}
{{ array.key }}
slugify
Converts a string to a slug.
{{ string|slugify }}
u
The u filter wraps a text in a Unicode object (a Symfony UnicodeString instance) that exposes methods to "manipulate" the string.
Example:
{{ 'cecil_string with twig'|u.camel.title }}
CecilStringWithTwig
singular
The singular filter transforms a given noun in its plural form into its singular version.
{{ string|singular(locale) }}
Example:
{# English (en) rules are used by default #}
{{ 'partitions'|singular }}
partition
{{ 'partitions'|singular('fr') }}
partition
plural
The plural filter transforms a given noun in its singular form into its plural version.
{{ string|plural(locale) }}
Example:
{# English (en) rules are used by default #}
{{ 'animal'|plural }}
animals
{{ 'animal'|plural('fr') }}
animaux
excerpt
Truncates a string and appends a suffix.
{{ string|excerpt(length, suffix) }}
| Option | Description | Type | Default |
|---|---|---|---|
| length | Truncates after this number of characters. | integer | 450 |
| suffix | Appends characters. | string | ' …' (space and ellipsis) |
Examples:
{{ variable|excerpt }}
{{ variable|excerpt(250, '...') }}
excerpt_html
Reads characters before or after <!-- excerpt --> or <!-- break --> tag.
See Content documentation for details.
{{ string|excerpt_html({separator, capture}) }}
| Option | Description | Type | Default |
|---|---|---|---|
| separator | String to use as separator. | string | excerpt|break |
| capture | Part to capture, before or after the separator. |
string | before |
Default values are defined by the pages.body.excerpt configuration.
Examples:
{{ variable|excerpt_html }}
{{ variable|excerpt_html({separator: 'excerpt|break', capture: 'before'}) }}
{{ variable|excerpt_html({capture: 'after'}) }}
highlight
Highlights a code string.
{{ code|highlight(language, engine) }}
The highlighter engine (highlightjs or tempest) is the optional engine argument or, by default, the layouts.highlight.engine option (highlightjs by default).
If the language is not supported by the engine, the code is returned escaped, without highlighting (and a warning is logged).
Examples:
{{ '<?php echo $highlighted->value; ?>'|highlight('php') }}
{{ '<?php echo $highlighted->value; ?>'|highlight('php', 'tempest') }}
preg_split
Splits a string into an array using a regular expression (limit is 0, i.e. no limit, by default).
{{ string|preg_split(pattern, limit) }}
Example:
{% set headers = page.content|preg_split('/<br[^>]*>/') %}
preg_match_all
Performs a regular expression match and returns the group for all matches (group is 0, i.e. the full match, by default).
{{ string|preg_match_all(pattern, group) }}
Example:
{% set tags = page.content|preg_match_all('/<[^>]+>(.*)<\/[^>]+>/') %}
hex_to_rgb
Converts a hexadecimal color to RGB: returns an array with red, green and blue keys.
{{ color|hex_to_rgb }}
Example:
{% set rgb = '#f00'|hex_to_rgb %}
rgb({{ rgb.red }}, {{ rgb.green }}, {{ rgb.blue }})
rgb(255, 0, 0)
splitline
Splits a string into an array of lines of max characters maximum (18 by default), without breaking words.
{{ string|splitline(max) }}
Example:
{% for line in 'The quick brown fox jumps'|splitline(10) %}
{{ line }}<br>
{% endfor %}
The quick
brown fox
jumps
iterable
Converts a variable to an iterable (array): an array is returned as is, a traversable object is converted to an array, and any other value is wrapped in an array.
{{ variable|iterable }}
Example:
{% for author in page.authors|iterable %}
{{ author }}
{% endfor %}
unique
Removes duplicate values from an array of strings (case-insensitive: the first occurrence is kept).
{{ array|unique }}
Example:
{{ ['PHP', 'php', 'Twig']|unique|join(', ') }}
PHP, Twig
duration_to_iso8601
Converts a duration in seconds to an ISO 8601 duration (e.g.: for the duration of a video in structured data).
{{ seconds|duration_to_iso8601 }}
Example:
{{ 125|duration_to_iso8601 }}
PT00H00M125S