Components

Atom's template syntax is designed for authoring rich server-rendered components. A component gives reusable markup its own PHP class and template. Its class defines the inputs, and its template uses expressions and directives to decide what HTML to render. Callers can pass values through attributes or supply markup for the component to place in its output.

Start with Atom Starter's SectionIntro, a heading with an optional label. Then use fragments to compose components with markup from the calling template.

Define the component

Extend TemplateComponent and declare the public properties the caller can fill. Defaults let callers omit the optional values.

app/Components/SectionIntro.php
<?php

declare(strict_types=1);

namespace App\Components;

use Atom\View\Component\TemplateComponent;

final class SectionIntro extends TemplateComponent
{
    public string $id = "";

    public string $eyebrow = "";

    public string $title = "";
}

Put the template next to the class. Atom finds SectionIntro.atom.html by its name, and $this inside that file refers to the SectionIntro instance.

app/Components/SectionIntro.atom.html
<header class="ui-section-intro">
    @if($this->eyebrow !== '')
        <p class="ui-eyebrow">{{ $this->eyebrow }}</p>
    @endif
    @if($this->id !== '')
        <h2 id="{{ $this->id }}">{{ $this->title }}</h2>
    @else
        <h2>{{ $this->title }}</h2>
    @endif
</header>

Register the tag

The name you register becomes the element name you use in templates. Atom Starter already registers SectionIntro. For a new component, add its entry to your application's existing components() hook.

app/Application.php (excerpt)
use App\Components\SectionIntro;
use Atom\View\Component\ComponentRegistry;

protected function components(ComponentRegistry $components): void
{
    $components->register("SectionIntro", SectionIntro::class);
}

Use the component in a template

Place the tag where the heading belongs. Literal attributes fill the component's properties with fixed text.

ProductPage.atom.html (excerpt)
<SectionIntro id="product-details" eyebrow="Catalogue" title="Product details" />

Use a colon-prefixed attribute to pass a value from the calling template. Here $this->product->name is evaluated on ProductPage. Atom assigns the result to SectionIntro::$title, then renders the component's template as HTML.

ProductPage.atom.html (excerpt)
<SectionIntro id="product-details" eyebrow="Catalogue" :title="$this->product->name" />

Accept child markup

A component can accept whole blocks of markup as well as attribute values. Atom Starter's Feature declares a public $content property of type ?Fragment. Atom assigns the markup between the opening and closing tags to that property.

app/Components/Feature.php (excerpt)
use Atom\View\Component\Fragment;

public ?Fragment $content = null;
Calling template
<Feature title="Delivery">
    <p>Check delivery options for {{ $this->product->name }}.</p>
    <a href="/delivery">Delivery details</a>
</Feature>

The expressions in the child markup use the calling template's scope. In the component template, fragmentHtml() renders that fragment as HTML inside the component's wrapper.

app/Components/Feature.atom.html (excerpt)
<div class="ui-feature__description">
    {{ $context->fragmentHtml($this->content) }}
</div>

Give callers named regions

Use named fragments when the caller needs to supply markup for several distinct regions. For example, define a DetailPanel with a plain-text title and separate fragments for the body and actions. Register DetailPanel in the same components() hook.

app/Components/DetailPanel.php
<?php

namespace App\Components;

use Atom\View\Component\Fragment;
use Atom\View\Component\TemplateComponent;

final class DetailPanel extends TemplateComponent
{
    public string $title = "";
    public ?Fragment $body = null;
    public ?Fragment $actions = null;
}
app/Components/DetailPanel.atom.html
<section>
    <h2>{{ $this->title }}</h2>
    {{ $context->fragmentHtml($this->body) }}
    @if($this->actions !== null)
        <footer>{{ $context->fragmentHtml($this->actions) }}</footer>
    @endif
</section>

Use the registered tag name followed by the region name. Atom maps DetailPanel.Body to $body and DetailPanel.Actions to $actions.

Calling template
<DetailPanel title="Delivery">
    <DetailPanel.Body>
        <p>Check delivery options for {{ $this->product->name }}.</p>
    </DetailPanel.Body>
    <DetailPanel.Actions>
        <a href="/delivery">Delivery details</a>
    </DetailPanel.Actions>
</DetailPanel>

The caller chooses what goes into each region. The component decides where those regions appear and can omit the footer when the caller supplies no actions.

Render HTML directly in PHP

The same DetailPanel can implement ComponentInterface and return HTML from render(). This works well for small wrappers whose markup fits in a few helper calls. Keep the registered DetailPanel tag and the same public properties so callers can use the examples above.

app/Components/DetailPanel.php
<?php

namespace App\Components;

use Atom\View\Component\ComponentInterface;
use Atom\View\Component\Fragment;
use Atom\View\Html;

final class DetailPanel implements ComponentInterface
{
    public string $title = "";
    public ?Fragment $body = null;
    public ?Fragment $actions = null;

    public function render(): string
    {
        $heading = Html::tag("h2", [], Html::escape($this->title));
        $body = $this->body?->render() ?? "";
        $footer = $this->actions === null
            ? ""
            : Html::tag("footer", [], $this->actions->render());

        return Html::tag("section", ["class" => "detail-panel"], $heading . $body . $footer);
    }
}

Html::tag() escapes attribute values. Its third argument is HTML content, so pass plain text through Html::escape() before inserting it. The body and actions come from rendered template fragments and can be inserted as HTML.

Render an adjacent PHP template

When there is more markup, keep it in an adjacent .atom.php file and delegate from render() to ComponentView::render($this). Keep the same registration and template call.

app/Components/DetailPanel.php
<?php

namespace App\Components;

use Atom\View\Component\ComponentInterface;
use Atom\View\Component\ComponentView;
use Atom\View\Component\Fragment;

final class DetailPanel implements ComponentInterface
{
    public string $title = "";
    public ?Fragment $body = null;
    public ?Fragment $actions = null;

    public function render(): string
    {
        return ComponentView::render($this);
    }
}

ComponentView looks for DetailPanel.atom.php beside the class. The PHP template can read public properties as local variables and use $context to escape text or render fragments. Use ordinary PHP expressions and control flow in this file.

app/Components/DetailPanel.atom.php
<section class="detail-panel">
    <h2><?= $context->encode($title) ?></h2>
    <?= $context->fragment($body) ?>
    <?php if ($actions !== null): ?>
        <footer><?= $context->fragment($actions) ?></footer>
    <?php endif; ?>
</section>

The component's public properties still define what callers can supply, regardless of which rendering approach you choose.

Forward extra HTML attributes

A caller may need to set a panel's id, add a CSS class, or label it for assistive technology. Declare a public AttributeBag on the component. Atom fills it with attributes that do not match a declared input, such as id, class, and aria-label. It still assigns title to $title.

In the .atom.html version of DetailPanel, use Html::mergeAttributes() to keep the root's default detail-panel class and add the caller's classes.

app/Components/DetailPanel.php
<?php

namespace App\Components;

use Atom\View\Component\AttributeBag;
use Atom\View\Component\Fragment;
use Atom\View\Component\TemplateComponent;
use Atom\View\Html;

final class DetailPanel extends TemplateComponent
{
    public string $title = "";
    public ?Fragment $body = null;
    public ?Fragment $actions = null;
    public AttributeBag $attributes;

    public function rootAttributes(): array
    {
        return Html::mergeAttributes(["class" => "detail-panel"], $this->attributes->all());
    }
}

Spread the resulting attributes onto the root <section> with {{ $this->rootAttributes() }} inside the opening tag. Atom renders and escapes the attribute values there.

app/Components/DetailPanel.atom.html
<section {{ $this->rootAttributes() }}>
    <h2>{{ $this->title }}</h2>
    {{ $context->fragmentHtml($this->body) }}
    @if($this->actions !== null)
        <footer>{{ $context->fragmentHtml($this->actions) }}</footer>
    @endif
</section>
Calling template
<DetailPanel title="Delivery"
             id="delivery-details"
             class="panel-highlight"
             aria-label="Delivery options">
    <DetailPanel.Body>
        <p>Standard delivery takes three business days.</p>
    </DetailPanel.Body>
</DetailPanel>

The root element renders as <section class="detail-panel panel-highlight" id="delivery-details" aria-label="Delivery options">. With no extra attributes, it keeps its default class.

Render a fragment for each list item

A list component can own the loop and the <li> elements while the caller supplies the markup inside each item. Declare the items as a public input and accept the row markup as the component's default Fragment.

app/Components/ItemList.php
<?php

namespace App\Components;

use Atom\View\Component\Fragment;
use Atom\View\Component\TemplateComponent;

final class ItemList extends TemplateComponent
{
    /** @var list<string> */
    public array $items = [];
    public ?Fragment $content = null;
}

Call fragmentHtml() inside the loop and pass the current value as ["item" => $item]. Each render adds $item to the caller's template scope. When the caller supplies no fragment, the component renders the item as escaped text.

app/Components/ItemList.atom.html
<ul>
    @foreach($this->items as $item)
        <li>
            @if($this->content !== null)
                {{ $context->fragmentHtml($this->content, ["item" => $item]) }}
            @else
                {{ $item }}
            @endif
        </li>
    @empty
        <li>No items to show.</li>
    @endforeach
</ul>

Register the tag in the application's components() hook.

app/Application.php (excerpt)
$components->register("ItemList", \App\Components\ItemList::class);

If ProductPage exposes $deliveryOptions as ["Standard delivery", "Express delivery"], the template can pass those values to the list. The <strong> markup is rendered once for each option.

ProductPage.atom.html (excerpt)
<ItemList :items="$this->deliveryOptions">
    <strong>{{ $item }}</strong>
</ItemList>

Use <ItemList :items="$this->deliveryOptions" /> for the plain-text version. The component controls the list structure in both cases.

Component property attributes

Atom supports two PHP attributes for wiring component inputs. They go on properties of the component class.

Attribute Where the value comes from
#[Children("Option", DeliveryOption::class)] Matching child tags inside the component invocation.
#[FromContext("item")] A variable named $item in the calling template's render scope.

Collect typed child components

Use #[Children] when child tags describe structured inputs. For example, a delivery list can collect Option elements into its $options array. Each tag becomes a DeliveryOption object with its own label input.

DeliveryList.php
<?php

namespace App\Components;

use Atom\View\Component\Children;
use Atom\View\Component\ComponentInterface;
use Atom\View\Html;

final class DeliveryList implements ComponentInterface
{
    /** @var list<DeliveryOption> */
    #[Children("Option", DeliveryOption::class)]
    public array $options = [];

    public function render(): string
    {
        $items = "";
        foreach ($this->options as $option) {
            $items .= $option->render();
        }

        return Html::tag("ul", [], $items);
    }
}
DeliveryOption.php
<?php

namespace App\Components;

use Atom\View\Component\ComponentInterface;
use Atom\View\Html;

final class DeliveryOption implements ComponentInterface
{
    public string $label = "";

    public function render(): string
    {
        return Html::tag("li", [], Html::escape($this->label));
    }
}

Register DeliveryList in components(). The #[Children] declaration tells Atom which class to use for Option.

Calling template
<DeliveryList>
    <Option label="Standard delivery" />
    <Option label="Express delivery" />
</DeliveryList>

Read a value from the calling template

Use #[FromContext] when the calling template already provides a value under a known name. The ItemList example provides $item each time it renders the row fragment. A component inside that fragment can use the value as its default label.

DeliveryLabel.php
<?php

namespace App\Components;

use Atom\View\Component\ComponentInterface;
use Atom\View\Component\FromContext;
use Atom\View\Html;

final class DeliveryLabel implements ComponentInterface
{
    #[FromContext("item")]
    public string $label = "";

    public function render(): string
    {
        return Html::tag("strong", [], Html::escape($this->label));
    }
}

Register DeliveryLabel, then use it inside the list's fragment.

Calling template
<ItemList :items="$this->deliveryOptions">
    <DeliveryLabel />
</ItemList>

Atom fills $label from $item on each iteration. An explicit attribute such as <DeliveryLabel label="Pickup" /> takes precedence. Calling #[FromContext] without a name looks up the property's own name in the render scope.

See Template syntax for attribute binding, escaped output, and conditional markup.