Utility Filters
General-purpose utility filters.
Default Value
| Filter | Arguments | Description |
|---|---|---|
default | default | Return fallback if empty |
Returns the fallback value if the input is empty, as the is empty test sees it: '', null, false, an empty array, or a query without results.
{{ post.meta('subtitle')|default('No subtitle') }}
{{ user.meta('description')|default('No biography available.') }}
{{ item.price|default(0) }}
{{ settings.color|default(theme.primary_color) }}
Note: The default filter treats false and 0 differently:
false→ returns default0→ returns0(not empty)
JSON Encoding
| Filter | Arguments | Description |
|---|---|---|
json_encode | flags | Encode value as JSON |
Arguments:
flags— JSON encoding options (default: 0)
{{ data|json_encode }}
{{ settings|json_encode }}
The JSON is escaped like any value, as in Twig: its quotes become ".
Pass data to JavaScript
Put the JSON in a data- attribute. The browser decodes the attribute, so the script reads valid JSON:
<div id="map" data-config="{{ settings|json_encode }}"></div>
const config = JSON.parse( document.getElementById( 'map' ).dataset.config );
Inside a <script> element nothing decodes the ", so var config = {{ settings|json_encode }}; breaks.
See Expressions & Syntax for a full comparison of ??, ?:, and |default.