Documentation

Template Syntax Reference

This guide covers all template syntax features in Clarity, including output expressions, directives, operators, and control structures.

Syntax Overview

Clarity uses two primary delimiters:

Syntax Purpose
{{ ... }} Output expressions (print values)
{% ... %} Directives (control flow, logic, inheritance)
{# ... #} Comments (not rendered in output)

Whitespace Control

A - glued to a delimiter suppresses the whitespace on that side of the tag: {%- trims the whitespace before the tag, -%} trims the whitespace after it. Only whitespace (spaces, tabs, newlines) is removed — visible text is not.

{% for item in items %}
  <li>{{ item }}</li>
{% endfor %}

Without control, each directive line leaves a blank line behind. Adding the markers collapses the loop to the markup alone:

{% for item in items -%}
  <li>{{ item }}</li>
{%- endfor %}

The same markers work on output tags ({{- x -}}) and comments ({#- c -#}).

Whitespace between tags

A whitespace-only run between two tags is a word gap when the line already carries content. It is kept as exactly one space:

<div class="card {{ cardClass ?? '' }} {{ bodyClass ?? '' }}">

renders class="card card-wide is-active". The same rule covers a directive as a neighbour — this is how a conditional class list is written:

<div class="card {% if wide %}card-wide{% endif %} {{ bodyClass ?? '' }}">

renders class="card card-wide is-active" when wide is true. A comment counts as a neighbour too. Use {{- … -}} / {%- … -%} to remove a gap you do not want.

A line break between two output tags is a word gap as well, so {{ a }}\n{{ b }} renders A B. A line break next to a directive is still layout: it disappears, which is what keeps a directive-only line from leaving a blank line behind.

A whitespace run that starts a line is indentation, not a gap, and is dropped as before. That is what keeps an {% extends %}/{% block %} preamble from gaining spaces, and what preserves the indentation of markup inside a <pre> block.

Output Expressions

Basic Output

Use double curly braces to output a value:

<p>{{ message }}</p>
<h1>{{ pageTitle }}</h1>

Auto-Escaping

All output is automatically HTML-escaped:

{{ userInput }}
<!-- If userInput = "<script>alert('xss')</script>" -->
<!-- Outputs: &lt;script&gt;alert('xss')&lt;/script&gt; -->

To output raw HTML, use the raw filter:

{{ trustedHtml |> raw }}

Variable Access

Access is strict and compile-time typed: the operator you write states what the value IS, and the engine emits exactly that read. There is no conversion of objects to arrays before rendering, so reading an object's state costs one property access and nothing else.

Syntax Meaning Emits
a.b.c object property (static) $vars['a']->b->c
a{expr} object property (dynamic) $vars['a']->{<expr>}
items[expr] array index $vars['items'][<expr>]
a:b:c array key (static) $vars['a']['b']['c']
$a->b PHP-style alias for . $vars['a']->b
$a->{<expr>} PHP-style alias for . $vars['a']->{<expr>}
a?.b a?[i] a?{k} a?:k optional receiver isset(…) ? … : null / …?->b
<!-- Object properties -->
{{ user.name }} {{ user.address.city }}

<!-- Array keys -->
{{ config:version }} {{ item:meta:title }}

<!-- Array index (dynamic) -->
{{ items[0] }} {{ items[index] }}

<!-- Dynamic property -->
{{ user{fieldName} }}

<!-- Mixed: array of objects -->
{{ users[0].profile.avatar }} {{ data:items[currentIndex].title }}

Strictness

Applying the wrong operator is an error, not a silent null:

{% set user = { name: "Alice" } %}   {# a MAP (PHP array) #}
{{ user.name }}     {# ERROR: Cannot read property "name" on array #}
{{ user:name }}     {# CORRECT #}

The same keys can be stated as an object literal when . is what you want to write — see Object literals:

{% set user = (object) { name: "Alice" } %}
{{ user.name }}     {# CORRECT #}

A missing key or property raises ClarityException naming the template and line.

With strictVariables denied, a missing name, key, property or intermediate value reads null at every level — so {% if user.nickname %} works exactly as {% if user %} does — and the diagnostic is reported as a warning carrying the template line instead of aborting the render. A read that applies the wrong operator (user.name on an array, user['x'] on an object) is still an error, because that is a bug in the template rather than data that happens to be absent.

Optional access

? marks the value BEFORE it as possibly absent or null. Everything after the ? is still strict, so a typo in the member is still an error:

{{ user?.name }}          {# fine when `user` is absent/null #}
{{ items?[0] }}           {# fine when `items` is absent/null #}
{{ user?.address?.city }} {# fine when `user` OR `address` is absent/null #}

The rule is ? guards the LEFT side, never the right one:

Expression user absent/null user present, name missing
user.name ERROR ERROR
user?.name null ERROR
user.name ?? 'x' ERROR 'x'
user?.name ?? 'x' 'x' 'x'

Emission differs per side because the operators differ in what they accept:

Expression Emits
a?:b (isset($vars['a']) ? $vars['a']['b'] : null)
a?.b (isset($vars['a']) ? $vars['a']->b : null)
a?.b?.c (isset($vars['a']) ? $vars['a']->b : null)?->c
a?:b?:c a === null test that binds the receiver to a temporary

Both sides use the shortest form that tolerates an absent receiver.

Array-side note. The array guard is a ternary, and a branch is evaluated before an outer operator sees it. items?[9] ?? 'fb' therefore cannot suppress a missing INDEX (the branch reads it first). Coalesce a STRICT read instead: items[9] ?? 'fb'. The object side has no such limit, because ?-> composes with a following ??.

The dollar sigil

Clarity accepts PHP variable syntax directly within templates. This allows you to use PHP-style property access and array indexing with the $ sigil.

-> is PHP-style property access and requires the $ sigil, so raw PHP syntax can never be emitted from an unsigiled expression:

{{ $user->name }}    {# legal, identical to {{ user.name }} #}
{{ user->name }}     {# COMPILE ERROR #}

: and the ternary

: continues a chain when it is followed by a key. Whitespace on either side is allowed, so all four of these read the same key:

{{ config:version }}
{{ config : version }}
{{ config: version }}
{{ config :version }}

The exception is a ternary, which also uses :. Once a ? has opened a branch, a colon only counts as a key when it is glued on both sides. That single rule is what separates the two without forbidding whitespace anywhere:

{{ cond ? "yes" : "no" }}   {# ternary #}
{{ cond ? "yes": "no" }}    {# ternary #}
{{ cond ? "yes" :"no" }}    {# ternary #}
{{ cond ? a:b : c }}        {# key read in the branch, then the separator #}

Two spellings are errors rather than ambiguity, because they differ from a valid form by one space:

{{ user ? : "fallback" }}   {# WRONG: empty then-branch (PHP's ?: shorthand) #}
{{ user?:nickname }}        {# CORRECT: optional key read #}

Named arguments are unaffected, because name: expr is consumed as an argument before expression compilation.

Nested conditions

Parenthesise a nested ternary and it nests to any depth:

{{ cond ? (foo ? bar : blubb) : blobb }}
{{ a ? (b ? (c ? d : e) : f) : g }}

A nested ternary in the THEN branch may omit the parentheses (a ? b ? c : d : e). One in the ELSE branch may not, because PHP rejects a ? b : c ? d : e outright rather than choosing an associativity:

{{ a ? b : (c ? d : e) }}   {# CORRECT: parenthesised else-branch #}
{{ a ? b : c ? d : e }}     {# COMPILE ERROR: use the form above #}

The same applies to an if/else chain written as one expression — use the parenthesised form, or a {% if %} / {% elseif %} block.

Whitespace and line breaks

Whitespace around a chain operator is not significant, so a long chain may wrap:

{{ user.
   address.
   city }}

{{ config:
   version }}

. is always property access, never string concatenation, so a chain has exactly one reading. Concatenation is ~:

{{ firstName ~ ' ' ~ lastName }}

An operator with no member after it (., ->) is a compile error. Optional access is the one operator that must stay glued, because a spaced ? is a ternary: user?:nick reads a key, while user ? x : y is a condition.

${expr} — dynamic variable access

${expr} reads the variable whose name is produced by an expression — the dynamic spelling of an ordinary variable access. $$name is the same construct (shorthand for ${name}).

{{ ${which} }}                {# the value of the variable named by {{ which }} #}
{{ $$which }}                 {# identical shorthand #}
{{ ${ 'a' ~ 'b' } }}          {# the name may be computed #}
{{ ${ref} ?? 'none' }}        {# absent name: fall back, like {{ name ?? 'x' }} #}
{{ ${which}.title }}          {# chained access applies to the looked-up value #}

``${expr}reads from the render scope and loop locals in every policy. An absent name throws unless you provide a?? fallback`. Dynamic names cannot access superglobals or engine internals.

Inside a string literal the $ is literal text: {{ "${which}" }} renders ${which}, not a lookup.

Iteration and container filters

Objects iterate their public properties, so {% for %} works over an object as well as an array. |> length, |> keys, |> values, |> first, |> last and |> reverse accept a container (array, Traversable, Countable, or an object exposing a public toArray()). A value object with no public state and a __toString() keeps STRING semantics, so |> length counts its characters.

Policy-Controlled PHP Access

Policies determine which PHP constructs templates can use; they do not change the template syntax. Registered filters and functions still take precedence. See The Policy API for the available rules and presets.

PHP functions as calls

These require the phpFunctions rule:

{{ strtoupper('ab') }}          {# \strtoupper('ab') #}
{{ implode(',', items) }}       {# \implode(',', $__c_va['items']) #}

PHP functions as filters

When the phpFunctions rule is on, an unregistered pipe step resolves to a PHP function of the same name. The piped value becomes the first argument:

{{ 'ab' |> strtoupper }}              {# \strtoupper($value) #}
{{ 'x' |> str_pad(3, '-') }}          {# \str_pad($value, 3, '-') #}

When the value does not belong first, a single _ placeholder positions it:

{{ 'k' |> array_key_exists(_, m) }}   {# \array_key_exists($value, $m) #}

_ may appear at most once as an argument in a PHP-function filter call when the phpFunctions rule is on; elsewhere _ keeps its meaning as an ordinary variable.

Method calls

A method call needs the methodCalls rule. Both styles work and compile to the same PHP — a template written in dot syntax works as good as one written in PHP-style syntax.

{{ user.name() }}                {# static method, dot syntax #}
{{ user?.name() }}               {# nullsafe #}
{{ obj{m}() }}                   {# dynamic method, brace syntax #}

{{ $user->name() }}              {# static method, PHP-style #}
{{ $user?->name() }}             {# nullsafe #}
{{ $user->{$method}() }}         {# dynamic method, PHP-style #}

Other calls stay rejected under every policy, because they are not method calls:

{{ $fn() }}                      {# ERROR: a call on the root value #}
{{ arr:greet() }}                {# ERROR: a call on a key read, not a member #}

Variable-driven calls are not allowed under any policy, and array-driven calls are also rejected.

Raw PHP tags

One spelling gives a trusted template the full power of PHP — one statement, or one fragment of a control structure, per tag:

{% php $total = 0; %}
{% php foreach ($items as $item) : %}
    {% php $total += $item['qty']; %}
{% php endforeach %}
{% php echo $total; %}

Splitting a control structure across tags is what lets PHP structure wrap template markup:

{% php if ($items) : %}
    <ul>
    {% php foreach ($items as $item) : %}
        <li>{{ $item['name'] }}</li>
    {% php endforeach %}
    </ul>
{% php else : %}
    <p>No items.</p>
{% php endif %}

A body may also span lines, for a run of statements in one tag:

{% php
$total = 0;
foreach ($items as $item) { $total += $item['qty']; }
echo $total;
%}

The closing delimiter is matched non-greedily, so a literal %} inside a body ends the tag early — spell it '%' . '}' when that exact sequence is needed.

Function guardrails

When the policy allows PHP access, PHP function calls from template expressions are available. Deny specific names with denyFunctions(); it does not inspect calls inside raw {% php %} blocks:

$engine->setPolicy(Policy::unrestricted()->denyFunctions('exec', 'system'));

Changing the list invalidates compiled templates whose policy digest no longer matches.

Directives

Directives use {% ... %} syntax for control flow and template structure.

Conditional Statements

If / Else / Elseif

{% if user.isActive %}
<span class="badge active">Active</span>
{% elseif user.isPending %}
<span class="badge pending">Pending</span>
{% else %}
<span class="badge inactive">Inactive</span>
{% endif %}

Single condition:

{% if stock > 0 %}
<button>Add to Cart</button>
{% endif %}

Loops

For Loop

Iterate over arrays:

<ul>
  {% for item in items %}
  <li>{{ item.name }} - {{ item.price |> number(2) }}</li>
  {% endfor %}
</ul>

Loop with Key Variable

Access both key and value in a single loop using the two-variable syntax. The first name is the key and the second is the value — the same order as Twig's {% for key, user in users %}:

{# Indexed array: index, value #}
{% for idx, item in items %}
<li>{{ idx }}: {{ item.name }}</li>
{% endfor %}

{# Associative array: key, value #}
{% for k, v in settings %}
<p>{{ k }}: {{ v }}</p>
{% endfor %}

This compiles directly to PHP's foreach ($items as $idx => $item).

Range Loops

Inclusive range (includes end):

{% for i in 1..10 %}
  {{ i }} {# Outputs: 1 2 3 4 5 6 7 8 9 10 #}
{% endfor %}

Exclusive range (doesn't include end):

{% for i in 1...10 %}
  {{ i }} {# Outputs: 1 2 3 4 5 6 7 8 9 #}
{% endfor %}

Range with step:

{% for i in 0..100 step 10 %}
  {{ i }} {# Outputs: 0 10 20 30 40 50 60 70 80 90 100 #}
{% endfor %}

Dynamic ranges:

{% for i in start..end step increment %}
  {{ i }}
{% endfor %}

Empty Sequences: {% else %}

A loop body may be followed by an {% else %} branch, rendered when the sequence is empty. This mirrors Twig and removes the need to count the items before the loop:

<ul>
  {% for user in users %}
  <li>{{ user.name }}</li>
  {% else %}
  <li class="empty">No users yet.</li>
  {% endfor %}
</ul>

It works for every loop form — over arrays, over mappings with for k, v, and over ranges:

{% for k, v in settings %}{{ k }}={{ v }}{% else %}no settings{% endfor %}

{% for i in 1..0 %}never{% else %}empty range{% endfor %}

Two rules follow from how Twig scopes loops:

  • The loop variable does not exist in the else branch. {{ user }} there resolves to a template variable of that name, if one exists, rather than to the loop's last value.
  • A loop takes one {% else %}. A second one — or an {% elseif %} — is a compile error.

Nesting: {% else %} belongs to the innermost open construct at the same nesting level. So in {% for %}…{% if %}…{% else %}…{% endif %}…{% else %}…{% endfor %} the first {% else %} is the if's and the second is the loop's. Conversely, in {% if %}{% for %}…{% endfor %}{% else %}{% endif %} the {% else %} closes the if, not the loop.

Plain {% else %} for Emptiness

Because the else branch runs only when nothing iterated, it is also the direct way to say "render the fallback":

{% for item in items %}{{ item }}{% else %}Nothing to show.{% endfor %}

Macros

Macros are reusable template fragments defined once and called multiple times within a template.

Defining a Macro

{% macro card(title, body) %}
<div class="card">
  <h3>{{ title }}</h3>
  <p>{{ body }}</p>
</div>
{% endmacro %}

Calling a Macro

Use {% call macroName(arg1, arg2) %} to invoke a macro:

{% call card("Welcome", "Hello from Clarity!") %}
{% call card(article.title, article.excerpt) %}

Default Parameter Values

A parameter may supply a default, which a call uses when it omits that argument. Optional parameters must come last:

{% macro terminal(label, command, icon = "fa-solid fa-terminal") %}
  <i class="{{ icon }}"></i> <span>{{ label }}</span>
  <pre><code>{{ command }}</code></pre>
{% endmacro %}

{% call terminal("Install", "composer require x") %}           {# default icon #}
{% call terminal("Install", "composer require x", "fa-bolt") %} {# own icon #}

A default is an ordinary expression, compiled where the call is — a macro body is spliced inline and has no scope of its own, so a default sees the caller's variables as well as the parameters already bound before it. Filters and calls work, and nested parentheses or quoted commas inside the default are handled:

{% macro badge(text = "new" |> upper) %}[{{ text }}]{% endmacro %}
{% macro row(label = slice("abcdef", 0, 3)) %}{{ label }}{% endmacro %}
{% macro titled(a, b = a ~ "!") %}{{ a }}{{ b }}{% endmacro %}  {# b reads a #}

Named Arguments

An argument may name its parameter: name: expr. It binds that parameter wherever it sits in the declaration, which is how a call skips a middle optional parameter without spelling out the ones before it:

{% macro terminal(label, command, icon = "fa-terminal", language = "") %}
  <i class="{{ icon }}"></i> <span>{{ label }}</span>
  <pre><code class="{{ language }}">{{ command }}</code></pre>
{% endmacro %}

{# icon keeps its default; language is set by name #}
{% call terminal("Install", "composer require x", language: "bash") %}

The argument rules are the ones every Clarity call uses:

  • Positional arguments must come before named ones, and each name must be unique. {% call t(a: 1, 2) %} and {% call t(a: 1, a: 2) %} both fail.
  • A name must be a parameter of that macro, so a typo is an error instead of a silent fall back to the default.
  • One parameter cannot be filled both ways. {% call t(1, a: 2) %} fails when a is the first parameter, but is fine when a is the second.
  • An argument is an ordinary expression. A colon-key read must be parenthesised to stay a read: {% call t((user:name)) %} passes the value of user.name-style key name; an unparenthesised user:name reads as a named argument of the parameter user, which is the rule the collections syntax already follows.

Named arguments work the same way on filters and functions (Filters and Functions).

Macro Rules

  • Define macros with {% macro name(param1, param2) %}...{% endmacro %}.
  • Call macros with {% call name(arg1, arg2) %}.
  • A parameter may declare a default value: {% macro name(a, b = "fallback") %}. A call that omits a trailing parameter uses its default; passing a value overrides it. Optional parameters must come last.
  • An argument may name its parameter (name: expr) to bind it out of order, which skips a middle optional parameter.
  • The definition itself renders nothing; the call expands at its own position.
  • Macros expand at compile time and accept expressions available at the call site.
  • Macro names are identifiers, so they are case-sensitive and may not be a directive keyword (if, for, set, block, include, …).
  • Recursive calls fail compilation.
  • Macros become available after their definition in the template or a static include. Independently rendered templates do not share them.
  • A macro may not be defined inside another macro's body. Macro names are not scoped, so a nested definition would be callable from anywhere in the template despite looking private — define it at the top level, or in an included macro library, instead.

Note: {% parent %}, which inlines a parent block's content in an overriding child block (Layout Inheritance), is a directive, not a macro — it takes no arguments and is not defined with {% macro %}.

Note: Macros used to be defined as {% macro @name %} and called as {% @name(args) %}, and {% parent %} used to accept an {% @parent %} spelling. Those forms are removed. A {% macro @name %} is refused with a message naming the replacement; an {% @name(args) %} or {% @parent %} tag is simply an unknown directive.

Multi-Parameter Example

{% macro avatar(name, size, href) %}
<a href="{{ href }}" class="avatar avatar--{{ size }}">
  <span>{{ name |> upper |> slice(0, 2) }}</span>
</a>
{% endmacro %}

{% for user in team %}
  {% call avatar(user.name, "md", "/users/" ~ user.id) %}
{% endfor %}

Variable Assignment

Set variables for reuse:

{% set total = items.length %}
{% set fullName = user.firstName ~ ' ' ~ user.lastName %}
{% set discount = price * 0.1 %}

<p>Total items: {{ total }}</p>
<p>Customer: {{ fullName }}</p>
<p>You save: {{ discount |> number(2) }}</p>

Assigned variables are scoped to the current template and blocks.

Template Inheritance

Extends

Extend a parent layout:

{% extends "layouts/main" %}

Must be the first directive in the template (before any output).

Blocks

Define overridable sections:

Parent template (layouts/main.clarity.html):

<!DOCTYPE html>
<html>
  <head>
    <title>{% block title %}Default Title{% endblock %}</title>
    {% block head %}{% endblock %}
  </head>
  <body>
    <header>
      {% block header %}
      <h1>My Site</h1>
      {% endblock %}
    </header>

    <main>{% block content %}{% endblock %}</main>

    <footer>
      {% block footer %}
      <p>&copy; 2026</p>
      {% endblock %}
    </footer>
  </body>
</html>

Child template (pages/about.clarity.html):

{% extends "layouts/main" %}

{% block title %}About Us{% endblock %}
{% block content %}
  <h2>About Our Company</h2>
  <p>We are awesome!</p>
{% endblock %}

Blocks not overridden in the child will use the parent's default content.

Parent Block Fallback

Inside an overriding child block, use {% parent %} to inline the parent block's content at that exact position during compilation:

{% extends "layouts/main" %}

{% block title %}
  Admin | {% parent %}
{% endblock %}

If the parent block contains My Website, the compiled result is Admin | My Website.

Rules:

  • Use {% parent %} only inside a child block that overrides a parent block.
  • It resolves at compile time and may appear more than once in a block.
  • In multi-level inheritance, it refers to the immediate parent block.

Includes

Static Include

Include another template, sharing the current variable scope:

{% include "partials/header" %}

<main>
  <!-- page content -->
</main>

{% include "partials/footer" %}

Included templates are inlined at compile time.

Tip: Static includes can act as macro libraries. If an included template defines macros, include it before calling those macros in the parent template.

Include with Namespaces

{% include "admin::sidebar" %}
{% include "emails::header" %}

See Advanced Topics for namespace configuration.

Operators

Comparison Operators

{% if age >= 18 %}
{% if status == 'active' %}
{% if count != 0 %}
{% if price < 100 %}
{% if score > 50 %}
{% if rating <= 5 %}
Operator Description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

Logical Operators

{% if user.isActive and user.role == 'admin' %}
{% if status == 'pending' or status == 'review' %}
{% if not user.isBlocked %}
Operator Description
and Logical AND
or Logical OR
not Logical NOT

Note: The symbols &&, ||, and ! are also accepted (they pass straight through to PHP).

Tests

Twig-style tests read as words and compile to registered callables, so they work under every policy. They can be used anywhere a boolean is expected.

{# membership — value in a list, substring in a string, key in a mapping #}
{% if 2 in [1, 2, 3] %}…{% endif %}
{% if 'ell' in word %}…{% endif %}
{% if role in user:roles %}…{% endif %}
{% if x not in items %}…{% endif %}

{# string tests #}
{% if name starts with 'Jo' %}…{% endif %}
{% if name ends with 'hn' %}…{% endif %}
{% if name matches '/^J.*n$/' %}…{% endif %}

{# numeric / identity tests #}
{% if n is even %}…{% endif %}
{% if n is odd %}…{% endif %}
{% if n divisible by 3 %}…{% endif %}
{% if a is same as(b) %}…{% endif %}
{% if value is iterable %}…{% endif %}

The absence-tolerant tests answer without reading their operand, so they are safe on a name that was never passed to the template:

{% if name is defined %}…{% endif %}
{% if name is not defined %}…{% endif %}
{% if nickname is null %}…{% endif %}
{% if items is empty %}…{% endif %}
{% if items is not empty %}…{% endif %}

nil is accepted anywhere null is, in both the value and the test position: {{ nil }}, {{ x is nil }}, {{ x ?? nil }} and {{ f(nil) }} all work.

is defined asks whether a name holds a value other than null. A name that was passed as null is reported as not defined, because the probe is PHP's isset(). Use is null to ask about the value instead; the two together still separate all three states:

user user is defined user is null
absent false true
null false true
'x', 0 true false

A test is an ordinary expression, so it composes with and, or, not, the ternary, and filters:

{% if role in user:roles and user is not null %}…{% endif %}
{{ 2 in [1, 2] ? 'yes' : 'no' }}
{{ (name |> lower) is defined ? 'set' : 'unset' }}

The tests starts with, ends with, divisible by and same as are two words. An unrecognised word after is (x is frobnicated) is a compile error.

Bitwise Operators

Use the keyword forms to avoid conflict with the | filter-pipe operator:

{{ flags bor mask }}    {# bitwise OR  — flags | mask  #}
{{ flags band mask }}   {# bitwise AND — flags & mask  #}
{{ flags bxor mask }}   {# bitwise XOR — flags ^ mask  #}
{{ bnot flags }}        {# bitwise NOT — ~flags        #}
{{ flags blsh 2 }}      {# bitwise left shift — flags << 2  #}
{{ flags brsh 2 }}      {# bitwise right shift — flags >> 2  #}
Operator PHP equivalent Description
bor | Bitwise OR
band & Bitwise AND
bxor ^ Bitwise XOR
bnot ~ Bitwise NOT
blsh << Bitwise left shift
brsh >> Bitwise right shift

Arithmetic Operators

{% set total = price + tax %}
{% set discount = price * 0.1 %}
{% set remaining = total - paid %}
{% set perItem = total / count %}
{% set remainder = total % 10 %}
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo

String Concatenation

Use the ~ operator:

{% set fullName = firstName ~ ' ' ~ lastName %}
{% set greeting = 'Hello, ' ~ user.name ~ '!' %}

<p>{{ 'Total: ' ~ total ~ ' items' }}</p>

Filter Pipe Operator

Both | and |> pipe a value through a filter. They are fully interchangeable:

{{ name | upper }}         {# Twig / Svelte style #}
{{ name |> upper }}        {# Clarity fat-pipe style #}

Whenever you need bitwise OR, use the bor keyword instead of | (see Bitwise Operators above).

|| (double pipe) is always logical OR and is never treated as a filter pipe.

Ternary Operator

{{ user.isActive ? 'Active' : 'Inactive' }}
{{ stock > 0 ? 'In Stock' : 'Out of Stock' }}
{{ age >= 18 ? 'Adult' : 'Minor' }}

Syntax: condition ? valueIfTrue : valueIfFalse

Conditions nest, but a nested ternary in the else-branch must be parenthesised — PHP rejects a ? b : c ? d : e outright. See Nested conditions.

{{ cond ? (foo ? bar : blubb) : blobb }}

Null Coalescing

{{ user.nickname ?? user.name }}
{{ customTitle ?? defaultTitle }}

Returns the right value if the left is null or undefined.

Expressions

Literal Values

{{ 42 }}
{{ 3.14 }}
{{ true }}
{{ false }}
{{ null }}
{{ "string literal" }}
{{ 'single quotes' }}

Cast Syntax

A PHP-style cast prefix converts a value's type. It is equivalent to the cast filters, written in PHP syntax:

{{ (int) x }}                    {# same as {{ x |> int }}        #}
{{ (float) '3.7' }}              {# 3.7                           #}
{{ (string) 1.5 }}               {# '1.5'                         #}
{{ (bool) value }}               {# true or false                 #}
{{ (array) value }}              {# [value]                       #}
{{ (object) value }}             {# scalar 1 becomes {"scalar":1} #}

The available types are int, float, string, bool, array and object. object converts a scalar or an array into an object.

The operand is a single value: a name, a chain ((int) user:age), an index ((int) x[0]), a literal ((int) -5), a parenthesized expression ((int) (10 + 5)), or another cast. Casts can be nested:

{{ (int) (float) x }}            {# 3.9 -> 3 #}

A cast can be followed by a pipe:

{{ (int) x |> string }}          {# cast, then filter #}

Cast or parenthesized expression

(int) and (a) have the same shape. The parser decides by what follows the closing parenthesis: if the parenthesized name is a cast type and an operand follows, it is a cast. Whitespace between the ) and the operand is optional.

Expression Reading
(int) x cast: whitespace, then a name
(int)x cast: same reading without whitespace
(int) -5 cast: whitespace, then a sign
(int)-5 cast: sign directly before a number
(int) (x) cast of the grouped x
(int)(x) cast of the grouped x, not a call
(int)[0] cast of the array [0], not an index
(a) + b not a cast: + cannot start an operand
(a) ? b : c not a cast: ? cannot start an operand
(a) x not a cast: existing reading is unchanged

An operand can start with an identifier, $, a quote, a digit, (, [, {, or a sign directly before a number. Otherwise the existing reading applies, so {{ (a) }}, {{ (a + b) }}, {% if (a) %} and {{ (a) ? b : c }} compile as before. Names that are not cast types are never treated as casts.

A - is a sign only when a digit or . follows it directly. This is why (int) -5 is a cast and (a) - 5 is subtraction.

Collection Literals

Arrays (lists — [] always holds values):

{% set numbers = [1, 2, 3, 4, 5] %}
{% set mixed = [true, "text", 42, user.name] %}

Maps (associative arrays — {} is a PHP array, read with : or [...]):

{% set person = { name: "John", age: 30, active: true } %}
{% set data = { id: item.id, title: item.title } %}

{{ person:name }} {{ person["age"] }}

A brace literal is an ARRAY, not an object. {{ person.name }} on the map above throws Cannot read property "name" on array — the strictness described under Variable Access.

Object literals — prefix the literal with the object cast. Inside it, a nested brace literal is another object literal, at any depth:

{% set person = (object) { name: "John", address: { city: "Rome" } } %}

{{ person.name }}            {# John                                    #}
{{ person.address.city }}    {# Rome — no cast needed at the nested level #}

This is a literal, so it costs nothing at runtime. It is also lexical, which keeps two neighbouring spellings meaning exactly what they read as:

Spelling Meaning
(object) { a: { b: 1 } } object, with a an object
{ a: { b: 1 } } map, with a a map — unchanged
{ a: { b: 1 } } |> object PHP's shallow cast: a stays a map
(object) { a: flag ? {b:1} : {} } a is a map: the literal is not the whole value

An array element is always a value, even inside an object literal:

{% set u = (object) { tags: ["a", "b"] } %}
{{ u.tags[1] }}              {# b #}

{} must stay an array: include("x", { title: "Hi" }), spread, merge, reverse and {% set x:key = … %} all require one. Reach for (object) only when . is what you want to write.

Spread operator in collections:

{% set extended = [1, 2, ...moreNumbers, 99] %}
{% set merged = { foo: "bar", ...otherData } %}

Built-in Functions

vars()

Returns all current template variables:

{% set allVars = vars() %} {{ allVars |> json }}

include()

Dynamically render another template at runtime:

{{ include("partials/card", { title: "Hello", ...vars() }) }}
{{ include(templateName, variables) }}

Unlike the {% include %} directive, this function:

  • Renders at runtime
  • Can use dynamic template names
  • Returns the rendered markup directly
  • Accepts custom context variables

Example:

{% for componentType in components %}
{{ include("components/" ~ componentType, { data: item }) }}
{% endfor %}

See Filters and Functions for custom functions.

Comments

Comments are removed during compilation and don't appear in output:

{# This is a comment #}
{# Multi-line comment
   Useful for documentation #}
{# TODO: Add pagination here #}

Context Hints

@context annotations in comments set the auto-escaping mode for following expressions. Clarity also detects context inside <script> and <style> tags; use an annotation to override the detected mode:

{# @context js #}
  var userData = '{{ user |> json }}';                        {# escaped string content …
                                                                 the quotes come from you #}
  var config   = JSON.parse('{{ config |> json |> raw }}');   {# valid JSON as an object #}
{# @context html #}
  <p>{{ message }}</p>                 {# Back to standard HTML escaping #}

Available contexts:

Context Escaping applied to output expressions
html htmlspecialchars() (default)
js quoted tag → escaped string content; unquoted tag → complete JSON literal
css Cast to (string) — no HTML escaping

Tip: Clarity automatically switches context when it encounters <script> or <style> tags in the template. Use {# @context … #} when the auto-detection isn't sufficient (e.g. inside a string literal that contains a tag-like pattern).

Inside <script>: two forms, chosen by the position

A script interpolation has two output forms, and where the tag sits selects between them:

<script>
var name = "{{ user.name }}";   {# -> var name = "Ada";          string content #}
var obj  = {{ user }};          {# -> var obj  = {"name":"Ada"};  JSON literal   #}
</script>

Quoted — you write the quotes, and the value arrives as escaped content:

<script>
var id   = '{{ user.id }}';     {# -> var id   = '7';     #}
var text = "hi {{ user.name }}";{# -> var text = "hi Ada"; #}
</script>

Unquoted — the value is a complete JSON literal, so strings arrive already quoted and numbers, booleans and arrays stay themselves:

<script>
var seats  = {{ license.seats }};    {# -> var seats  = 3;          #}
var paid   = {{ order.paid }};       {# -> var paid   = true;       #}
var tiers  = {{ tiers }};            {# -> var tiers  = [1,2,3];    #}
var config = {{ config }};           {# -> var config = {"a":1};    #}
</script>

No coercion is needed in either direction: Number(…), === '1' and JSON.parse(…) are no longer required, though JSON.parse('{{ x |> json |> raw }}') still works if you prefer it.

The risk, stated plainly

Choosing between two forms means the choice can be wrong, and a wrong choice changes what is rendered. That is the price of allowing both forms; it was taken because an unquoted tag is the natural way to write a number or a JSON payload.

What keeps it safe is that a misjudgement fails loudly, never silently:

Misjudgement Output Result
quoted tag read as code var x = {{ v }}; → var x = "Ada"; inside your "…" ""Ada"" — JS syntax error
code tag read as string var x = {{ n }}; → var x = 3; inside a string position escaped content, still a valid literal
a number, either way 3 and the content of 3 are both 3 identical, by design

And measured, not assumed: across 72 combinations of both forms and hostile values (quotes, backticks, ${…}, </script>, newlines, NUL bytes, BOM, emoji) every rendered script is valid JavaScript, and no value containing ;alert(1) became code — in either form, and with comments, regex literals and template literals before it.

Positions that are rejected

A tag inside a regular expression or directly inside a template literal has no sane output form, so both are a compile error:

<script>
var r = /{{ v }}/;      {# ClarityException — not meaningful in a pattern #}
var t = `{{ v }}`;      {# ClarityException — use "{{ v }}" or ${ {{ v }} } #}
var ok = `${ {{ v }} }`;{# fine: back inside JavaScript, so JSON literal #}
</script>

raw keeps its universal meaning (verbatim, no escaping) in every context, which is what lets it bypass all of the above: {{ x |> json |> raw }} is emitted untouched, quoted or not.

What the scan understands

The scanner reads JavaScript's own comments, regular expressions and template literals, so a quote in prose is never mistaken for a delimiter:

<script>
// don't touch this: "{{ value }}"      {# apostrophe in a line comment #}
var t = `it's`; var a = "{{ value }}"; {# apostrophe in backticks #}
var re = /"/; var a = "{{ value }}";   {# quote in a regex literal #}
var d = a / 2; var s = "{{ value }}";  {# division, not a regex #}
</script>

Two known limits, both confined to one line because a raw line break cannot occur inside a JS string literal:

  • `{{ value }}` — a tag in template-literal text is rejected (see above);
  • a comment opener split across two text segments (a / ending one segment and a * starting the next). Move the expression and the check passes.

The scan is deliberately fail-safe: a wrong guess can only reject a template that would have worked, never emit a bare value into a script.

Complex Examples

Nested Loops

<table>
  {% for category in categories %}
  <tr>
    <th>{{ category.name }}</th>
    <td>
      <ul>
        {% for product in category.products %}
        <li>{{ product.name }} - {{ product.price |> number(2) }}</li>
        {% endfor %}
      </ul>
    </td>
  </tr>
  {% endfor %}
</table>

Conditional Rendering with Loops

{% if users.length > 0 %}
  <ul>
    {% for user in users %} {% if user.isActive %}
    <li class="active">{{ user.name }}</li>
    {% endif %} {% endfor %}
  </ul>
{% else %}
  <p>No active users found.</p>
{% endif %}

Complex Variable Assignment

A brace literal is a map, so it is read with :. Prefix it with (object) when the dotted spelling reads better:

{% set userData = (object) {
  fullName: user.firstName ~ ' ' ~ user.lastName,
  age: user.birthYear ? (2026 - user.birthYear) : null,
  isAdult: user.birthYear and (2026 - user.birthYear) >= 18
} %}

<p>Name: {{ userData.fullName }}</p>
{% if userData.isAdult %}
  <p>Age: {{ userData.age }}</p>
{% endif %}

Without the cast, the same literal is a map and userData.fullName is an error (Cannot read property "fullName" on array); userData:fullName is the read.

Dynamic Includes

{% for widget in dashboard.widgets %}
  {{ include("widgets/" ~ widget.type, {
    title: widget.title, data: widget.data, config: widget.config }) }}
{% endfor %}

Next Steps

Quick Reference

Directive Summary

Directive Purpose
{% if condition %} Conditional rendering
{% elseif condition %} Alternative condition
{% else %} Fallback case
{% endif %} End conditional
{% for item in array %} Loop over array
{% for key, value in array %} Loop with key variable
{% for i in start..end %} Range loop (inclusive end)
{% for i in start...end %} Range loop (exclusive end)
{% else %} (inside a loop) Runs when the loop is empty
{% endfor %} End loop
{% set variable = value %} Variable assignment
{% extends "template" %} Inherit from layout
{% block name %}...{% endblock %} Define/override block
{% include "template" %} Include another template
{% macro name(params) %}...{% endmacro %} Define a reusable macro
{% call name(args) %} Call a macro
{% call name(a, b: value) %} Call a macro with a named argument (skips middle optionals)
{% macro name(a, b = value) %} Macro with a default value
{% parent %} Inline parent block content
{# comment #} Template comment
{# @context js|css|html #} Switch escaping context

Operator Summary

Category Operators
Comparison == != < > <= >=
Logical and or not
Tests in not in is defined is null is empty is iterable
is even is odd starts with ends with matches
divisible by same as
Arithmetic + - * / %
Bitwise band bor bxor bnot blsh brsh & ^ ~ << >>
String ~ (concatenation)
Ternary condition ? true : false
Null coalesce ??
Spread ... (in arrays/objects)