Cecil logo Cecil
What's on this page

Functions

Functions can be called to generate content. Functions are called by their name followed by parentheses (()) and may have arguments.

url

Creates a valid URL for a page, a menu entry, an asset, a page ID or a path.

{{ url(value, {options}) }}
Option Description Type Default
canonical Prefixes URL with baseurl or uses canonical.url if it exists. boolean value of canonicalurl (false by default)
format Defines page output format (e.g.: json). string html
language Defines page language (e.g.: fr). string current language

Examples:

{# page #}
{{ url(page) }}
{{ url(page, {canonical: true}) }}
{{ url(page, {format: 'json'}) }}
{{ url(page, {language: 'fr'}) }}
{# menu entry #}
{{ url(site.menus.main.about) }}
{# asset #}
{{ url(asset('styles.css')) }}
{# page ID #}
{{ url('page-id') }}
{# path #}
{{ url('about-me/') }}
{{ url('tags/' ~ tag) }}

html

Creates an HTML element from an asset (or an array of assets with custom attributes).

{{ html(asset, {attributes}, {options}) }}
{# dedicated functions for each common type of asset #}
{{ css(asset) }}
{{ js(asset) }}
{{ image(asset) }}
{{ audio(asset) }}
{{ video(asset) }}
Argument Description Type
attributes Adds name="value" couple to the HTML element. array
options {preload: boolean}: preloads.
For images:
{formats: array}: adds alternative formats.
{responsive: bool|string}: adds responsive images (based on width or pixels density).
{placeholder: string}: fills the image background before loading (color or lqip).
array

Examples:

{# CSS with an attribute #}
{{ html(asset('print.css'), {media: 'print'}) }}
{# CSS with an attribute and an option #}
{{ html(asset('styles.css'), {title: 'Main theme'}, {preload: true}) }}
{# Array of assets with media query #}
{{ html([
  {asset: asset('css/style.css')},
  {asset: asset('css/style-dark.css'), attributes: {media: '(prefers-color-scheme: dark)'}}
]) }}
{# JavaScript #}
{{ html(asset('script.js')) }}
{# image without specific attributes nor options #}
{{ html(asset('image.png')) }}
{# image with specific attributes, responsive images and alternative formats #}
{{ html(asset('image.jpg'), {alt: 'Description', loading: 'lazy'}, {responsive: true, formats: ['avif', 'webp']}) }}
{# image with responsive pixels density images #}
{{ html(asset('image.jpg'), options={responsive: 'density'}, attributes={width: 256}) }}
{# image with a Low-Quality Image Placeholder #}
{{ html(asset('image.jpg'), {alt: 'Description', loading: 'lazy'}, {placeholder: 'lqip'}) }}
{# Audio #}
{{ html(asset('audio.mp3')) }}
{# Video #}
{{ html(asset('video.mp4')) }}

readtime

Determines read time of a text, in minutes (based on 200 words per minute, 1 minute minimum).

{{ readtime(value) }}

Example:

{{ readtime(page.content) }} min

hash

Calculates the hash of an object, an array or a string with a given algorithm.

{{ hash(value, algorithm) }}

algorithm can be any algorithm supported by PHP’s hash() function (e.g.: md5, sha256, etc.). Default is xxh128.

Example:

{{ hash('my string', 'sha256') }}

cache_key

Calculates a cache key for fragments cache based on a name and an optional value.

{% cache cache_key(name, value) %}
  {# cacheable content #}
{% endcache %}

The function adds a hash of the value (could be a string, an array or an object) to the name (and the current language and build ID to be sure the generated cache key is unique), so if the value changes, the cache key changes too and a new cache entry is generated.

getenv

Gets the value of an environment variable from its key.

{{ getenv(var) }}

Example:

{{ getenv('VAR') }}

dump

The dump function dumps information about a template variable. This is mostly useful to debug a template that does not behave as expected by introspecting its variables:

{{ dump(user) }}

d

The d() function is the HTML version of dump() and uses the Symfony VarDumper Component behind the scenes.

{{ d(variable, {theme: 'dark'}) }}
  • If variable is not provided, the function dumps the current Twig context
  • Available themes are light (default) and dark

Tests

Tests can be used with the is operator.

Test Description
asset The variable is an asset.
image_large The asset is a landscape image of at least 600 × 315 pixels (e.g.: a cover image).
image_square The asset is a square image (same width and height).
enabled The option is enabled: true, a map without enabled key or with enabled: true, or any other value except false and null.
external The URL (or the URL of a menu entry) is external: it has a scheme (https:, mailto:, etc.) or starts with //, and does not start with baseurl.

Example:

{% set image = asset(page.image) %}
{% if image is image_large %}
  {{ html(image, {alt: page.title}) }}
{% endif %}
{% if config.cache is enabled %}
  {# `cache: false` and `cache: {enabled: false}` are both disabled #}
{% endif %}
{% for item in site.menus.main|sort_by_weight %}
  <a href="{{ url(item.url) }}"{% if item is external %} rel="noopener" target="_blank"{% endif %}>{{ item.name }}</a>
{% endfor %}

Deprecated

The following functions are deprecated (a deprecation notice is triggered when they are used):

Function Replacement
minify() minify filter
toCSS() to_css filter
image_from_url() image_from_website() function