Template syntax

Atom parses .atom.html files with its own template parser. It turns the source into an abstract syntax tree (AST), with nodes for elements, text, expressions, conditions, and loops. When Atom renders the template on the server, it evaluates expressions and follows the conditional and loop nodes to produce HTML.

Feature Syntax What it does
Escaped output {{ $value }} Render an expression as text.
Attribute interpolation title="Hello {{ $name }}" Combine literal text and an expression in an attribute.
Attribute binding :disabled="$locked" Use an expression as the entire attribute value.
Attribute spread {{ $attributes }} inside an opening tag Add attributes from a map.
Conditional blocks @if, @elseif, @else, @endif Render one branch of markup.
Conditional elements :if, :else-if, :else Apply a condition to a whole element.
Loop blocks @foreach, @empty, @endforeach Repeat a block or render its empty branch.
Loop elements :foreach, :empty Repeat a whole element or render an empty-state sibling.

Render page data

In a page template, $this and $page refer to the page instance. An expression can read a property or call a method. Atom escapes its output, so angle brackets in a name appear as text.

<h1>{{ $this->heading }}</h1>
<p>{{ $this->statusLabel() }}</p>

Set attributes

Interpolate a value with {{ ... }} inside a quoted attribute. Use the colon prefix when the expression supplies the whole value. For a boolean attribute such as disabled, true includes the attribute and false omits it.

<button type="button" title="Open {{ $this->heading }}" :disabled="$this->locked">
    Open
</button>

To add several attributes together, put an expression inside the opening tag. Here $this->linkAttributes can supply a map such as ["href" => "/docs", "class" => "help-link"].

<a {{ $this->linkAttributes }}>Help</a>

Directives

Use a conditional block when a branch can contain several elements. Atom renders the first matching branch, or the optional @else branch if none matches.

@if($this->count > 1)
    <p>Several results</p>
@elseif($this->count === 1)
    <p>One result</p>
@else
    <p>No results</p>
@endif

For a condition on a whole element, use structural attributes. Keep :else-if and bare :else on adjacent siblings.

<p :if="$this->count > 1">Several results</p>
<p :else-if="$this->count === 1">One result</p>
<p :else>No results</p>

Repeat markup

Use @foreach with as $value or as $key => $value. The optional @empty branch runs when the iterable yields no items. Loop variables are available inside the repeated block.

<ul>
    @foreach($this->items as $key => $label)
        <li>{{ $key }}: {{ $label }}</li>
    @empty
        <li>No items yet.</li>
    @endforeach
</ul>

To repeat a whole element, use :foreach. A bare :empty on the next sibling provides the fallback. To combine a condition and a loop, nest elements and put one structural attribute on each.

<ul>
    <li :foreach="$this->items as $label">{{ $label }}</li>
    <li :empty>No items yet.</li>
</ul>