Skip to content

Twig filters, functions and variables

This page lists what ClientFence adds to Twig, so you can use it in a theme of your own, a template in html_custom or a module. Twig’s own tags, filters, functions and tests work as the Twig documentation describes, except where a sandbox limits them.

Where What it can use
Client area and admin area pages Everything on this page.
Email templates and the mass mailer A sandbox: a short list.
Theme settings pages and the payment text of manual payment gateways A sandbox: a short list.

Pages run with strict variables: a variable that does not exist stops the page with an error. Use |default or is defined:

{{ settings.banner_text|default('') }}
{% if profile is defined and profile %}…{% endif %}

On client and admin area pages:

Variable What it holds
guest The guest API. Always there. See API calls.
client The client API while a client is signed in, else null.
admin The admin API while a staff member is signed in in this browser, else null.
app_area client or admin.
current_theme The active theme’s folder name, for example palisade.
settings Client area only: the active preset’s theme settings.
theme Admin area only: theme.code and theme.url of the admin theme.
maintenance_mode_on Admin area only: true while maintenance mode is on.
csp_nonce This page’s script nonce, for <script nonce="{{ csp_nonce }}">.
CSRFToken The session’s form token, for <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}">.
request The query string values, read only. A missing key gives null. request.ajax is true for a background request.
request_query The query string values as a plain array.
request_path The current page’s path.
request_has_filters true when the query string has more than page and search.
flashes Messages saved in the session for the next page.
redirect_uri Defined only when the session holds an address to go back to.
default_currency The default currency’s code, for example USD, or null.
ClientFenceVersion The installed version, for example 1.0.57.

A page also gets the variables its controller passes and those it sets itself, such as active_menu.

guest, client and admin call the API while the page is built. The name is the module, an underscore and the method; parameters go in one hash:

{{ guest.system_company.name }}
{% set profile = client ? client.profile_get : null %}
{% set themes = admin.theme_get_list({ 'per_page': 100 }) %}
{% if guest.extension_is_on({ mod: 'seo' }) %}…{% endif %}

Each call runs with the same checks as the API: an admin call needs the staff member’s permission for that module, an authorized client user gets only what their permissions allow, and a call to a module that is not active fails. Test client and admin before using them: they are null for visitors.

Filter Arguments What it does Example
trans values Translates the text and replaces each key of values. {{ 'Hello :name'|trans({ ':name': profile.first_name }) }}
trans_html values, html Translates a sentence that holds markup. The translation and values are escaped; html is inserted as it is. {{ 'Read the :terms.'|trans_html({}, { ':terms': '<a href="/https/clientfence.com/tos">' ~ ('terms'|trans|e) ~ '</a>' }) }}
url query, area A full link to a page of the current area (area: client or admin). {{ 'invoice'|url }}, {{ 'signup'|url({ back: '/cart' }) }}
asset_url A file in the active theme’s assets/ folder. {{ 'css/mybrand.css'|asset_url }}
public_asset_url A file in public/assets/. {{ 'js/api.js'|public_asset_url }}
script_tag A <script> tag with the nonce and a version, once per page. {{ 'js/mybrand.js'|asset_url|script_tag }}
stylesheet_tag media (default screen) A stylesheet <link> with a version, once per page. {{ 'css/mybrand.css'|asset_url|stylesheet_tag('all') }}
format_currency currency, attrs, locale An amount in a currency, with that currency’s own format and decimals when set. Arabic pages use the English money format. {{ invoice.total|format_currency(invoice.currency) }}
daysleft Whole days from now to a date; negative when past. {{ order.expires_at|daysleft }}
timeago Time since a date, such as 3 days (no “ago”); - for a future date. {{ ticket.updated_at|timeago }}
file_size Bytes as a readable size. {{ file.size|file_size }}
truncate length (30), suffix (...) Shortens a text. {{ ticket.subject|truncate(40) }}
hash algo (xxh128) A hash of the value with any PHP algorithm. {{ email|hash('sha256') }}
json_encode options JSON in which < > & ' " are always escaped, so it is safe inside a script. {{ data|json_encode|raw }}
with_csp_nonce Adds the nonce to each <script> of trusted HTML. Never use it on text anyone else typed. {{ settings.inject_javascript|with_csp_nonce }}
Filter Arguments What it does Example
period_title A billing period’s name. {{ '1M'|period_title }} gives “Every Month”
ip_country_name The country of an IP address, or an empty text. {{ event.ip|ip_country_name|default('Unknown'|trans) }}
ip_country_code The same as a lowercase two-letter code. {{ event.ip|ip_country_code }}
mod_asset_url module A file in a module’s assets/ folder. {{ 'logo.png'|mod_asset_url('servicehosting') }}
Filter Arguments What it does Example
api_url query, role The address of an API call; role is client, admin or guest and follows the current area. {{ 'client/balance_alert_save'|api_url }}
Filter What it does
format_date, format_datetime, format_time A date in the visitor’s language and time zone. On pages, Arabic, Hebrew and Persian dates stay in one piece.
format_number, format_*_number A number in the visitor’s language, for example format_percent_number.
country_name, currency_name, currency_symbol, language_name, locale_name, timezone_name A code as a name, for example {{ 'DE'|country_name }}.
format_list A list joined in the visitor’s language. Needs PHP 8.5.
markdown_to_html Markdown as HTML. HTML in the text is escaped and unsafe links are dropped. The theme’s markdown_attributes add CSS classes.

html_to_markdown is registered but fails: the library it needs is not installed.

Function Arguments What it does Example
render_widgets slot, context The module widgets for a slot. {{ render_widgets('client.theme.footer.end') }}
svg_sprite The theme’s icon sprite, to put once in the page. <div class="d-none">{{ svg_sprite() }}</div>
has_permission module, permission Whether the signed-in staff member has a permission; false for others. {% if has_permission('security', 'view') %}
antispam_honeypot enabled and field of the anti-spam trap field. {% set honeypot = antispam_honeypot() %}
avatar email, size (40), classes, fallback, tag A generated picture for an email address. {{ avatar(profile.email, 32, 'avatar avatar-sm') }}
wysiwyg selector, options Turns a text area into a rich text editor. {{ wysiwyg('.editor-textarea') }}
fb_api config A data-fb-api attribute, for a form or a link (other elements are not bound). <a href="…" {{ fb_api({ … }) }}>
fb_api_form config The attributes that send a form to the API in the background. <form action="{{ 'client/balance_alert_save'|api_url }}" {{ fb_api_form({ message: 'Saved'|trans }) }}>
fb_api_link config The same for a link. <a href="{{ 'client/resend_email_verification'|api_url }}" {{ fb_api_link({ message: 'Sent'|trans }) }}>
debug_bar_render_head, debug_bar_render The debug bar, in a development environment only; empty otherwise. Its tags carry the page’s nonce.
country_names, currency_names, language_names, locale_names, script_names, timezone_names, country_timezones Lists of codes and names in the visitor’s language. {% for code, name in country_names() %}
template_from_string template Renders Twig text as a template.
dump values Shows variables, only when Twig debug is on in config.php.

The config keys of fb_api, fb_api_form and fb_api_link are in the JavaScript API. The API script reads them; a form sent this way gets the form token by itself.

ClientFence adds no tests. Twig’s own work, such as is defined, is empty, is null and is iterable.

Email templates and the mass mailer run in a sandbox. Only these work:

Kind Allowed
Tags if, for, block, apply, set
Filters escape, e, default, title, length, date, first, format_currency, format_date, format_datetime, format_number, format_time, currency_name, currency_symbol, country_name, url, daysleft, trans, trans_html, period_title, markdown_to_html
Functions country_names
Methods and properties of objects None: email variables are plain values and lists.

Global variables: guest.system_company (company details), guest.system_email.signature, default_currency, ClientFenceVersion, locale (for example de-DE) and dir (rtl or ltr). url gives a client area link. Anything else stops the email with an error that names it. Each email template’s own variables are in Email templates.

A theme’s config/settings.html.twig and the payment text of the manual (custom) payment gateways run in another sandbox:

Kind Allowed
Tags if, for, block, apply, set, spaceless
Filters escape, e, default, title, length, date, format_currency, format_date, format_datetime, format_number, format_time, currency_name, currency_symbol, country_name, url, daysleft, trans, period_title, number_format, round, abs, json_encode, trim, lower, upper, nl2br, striptags, join, split, sort, merge, reverse, keys
Functions country_names

A settings page gets settings and ClientFenceVersion; a payment text gets invoice, guest.system_company and ClientFenceVersion. The result is cleaned: plain HTML stays (and form fields on a settings page); <script>, <style> and style attributes are removed.

|trans looks the English text up in the active language. Placeholders start with a colon:

{{ ':count item(s) in your cart'|trans({ ':count': cart_count }) }}

Texts that ClientFence already uses come with its translations. A new text of your own stays in English until you add a translation: ship it in a module’s locale/ folder (see Translate it).