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) }} Example:
{{ pages|filter_by('section', 'blog') }} filter
For more complex cases, you should use Twig’s native filter.
Example:
{% pages|filter(p => p.virtual == 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 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 suffix.
{{ string|excerpt(length, suffix) }} | Option | Description | Type | Default |
|---|---|---|---|
| length | Truncates after this number of characters. | integer | 450 |
| suffix | Appends characters. | string | … |
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 |
Examples:
{{ variable|excerpt_html }}
{{ variable|excerpt_html({separator: 'excerpt|break', capture: 'before'}) }}
{{ variable|excerpt_html({capture: 'after'}) }} highlight
Highlights a code string with highlight.php.
{{ code|highlight(language) }} Examples:
{{ '<?php echo $highlighted->value; ?>'|highlight('php') }} preg_split
Splits a string into an array using a regular expression.
{{ string|preg_split(pattern, limit) }} Example:
{% set headers = page.content|preg_split('/<br[^>]*>/') %} preg_match_all
Performs a regular expression match and return the group for all matches.
{{ string|preg_match_all(pattern, group) }} Example:
{% set tags = page.content|preg_match_all('/<[^>]+>(.*)<\/[^>]+>/') %} hex_to_rgb
Converts a hexadecimal color to RGB.
{{ color|hex_to_rgb }}