---
title: Usage
weight: 4
---
```rust
use rustysan_sword::Sword;
use serde_json::json;
let html = Sword::default().render_source(
"greeting", "Hello {{ name }}", &json!({"name": "<Ada>"})
)?;
assert_eq!(html, "Hello <Ada>");
# Ok::<(), rustysan_sword::Error>(())
```
`Sword::render` resolves dotted names to `.sword.html` under ordered template roots, defaulting to `resources/views`; see [Configuration](#configuration). Rendering reads each visited source on every call and uses a bounded in-memory artifact cache; there is no persistent template cache. Includes, components, and layouts are loaded when execution reaches them, so an include in an untaken branch is not resolved. Visited cycles return a typed error, and visited dependency chains are limited to 64 templates. Lookup rejects path separators and traversal in names.
Sword supports `<x-name>` and `<x-name />` components looked up as `components.name`, with quoted literal props, `:prop` expression bindings, a default `slot`, and `<x-slot name="header">` or `<x-slot:header>` named slots. Colon attributes such as `rusty:click="Save"` are forwarded through the attribute bag; prefix with `:` to bind a value. They are attributes, not component props. Props and slots are local to the component invocation; the caller’s variables remain visible, and slot bodies evaluate in the caller’s scope before component props are applied. Slot markup is rendered as trusted HTML after interpolation is escaped. Components may declare defaults with `@props(['type' => 'button'])`. Render `{{ attributes }}` or merge defaults with `{{ attributes.merge(['class' => 'base']) }}`; declared props are excluded, caller attributes override defaults, and classes are combined. Attribute names and values are escaped or validated. This initial merge syntax accepts a comma-separated list of `'name' => expression` pairs; commas inside quoted strings or brackets belong to the expression, and a trailing comma is allowed.
It also supports escaped interpolation, template comments, `@@`, escaped echoes (`@{{ ... }}`), `@verbatim` / `@endverbatim`, `@if` / `@elseif` / `@else`, `@unless` / `@endunless`, `@switch` / `@case` / `@default` / `@endswitch`, `@isset` / `@endisset`, standalone `@empty` / `@endempty`, conditional attributes (`@checked`, `@selected`, `@disabled`, `@readonly`, `@required`), `@class` and `@style`, `@foreach`, loop `@break` / `@continue`, `@forelse` / `@empty`, `@include`, `@includeIf`, `@includeWhen`, `@includeUnless`, `@includeFirst`, `@each`, `@extends`, `@section` / `@endsection` / `@stop` / `@show` / `@append` / `@overwrite`, `@yield`, `@parent`, `@hasSection` / `@sectionMissing`, `@csrf`, `@method`, `@error` / `@enderror`, `@fragment` / `@endfragment`, `@push` / `@endpush`, `@prepend` / `@endprepend`, `@pushOnce` / `@prependOnce` and their closers, `@stack`, and `@once` / `@endonce`. Includes inherit their caller’s scope. View names are quoted literals. An optional data object such as `@include('card', {'title': page.title})` adds names to that scope for the included view only; its keys are quoted literals and its values are expressions evaluated in the caller’s scope before any key is bound. `@includeIf` skips a missing view, `@includeWhen(condition, ...)` and `@includeUnless(condition, ...)` include on a condition, `@includeFirst(['a', 'b'])` takes the first view that exists and fails with a clear error if none does, and `@each('row', items, 'item', 'empty')` renders a view per element of a sequence or map with `item` and `key` bound, then the empty view or, for `'raw|text'`, that text unescaped. Conditions and data are not evaluated, and views not opened, in a branch that does not run. `@each` shares the caller’s scope rather than starting an empty one. Sections: `@yield('name', default)` prints the section or the escaped default; `@section('name', content)` is the inline form; `@parent` marks where the layout's version goes, and a marker nothing resolves prints nothing; `@append` and `@overwrite` act on what an earlier definition left; `@hasSection` ignores whitespace-only sections. Parent markers carry a per-render token, so view data cannot forge one. `@else` and `@elseif` also work inside `@isset`, `@empty(...)` and `@unless`. Layouts and includes search roots in order; Laravel resolves them through its view finder. Section names must be identifiers. The supported expression contract is described below. Function calls, filters, raw backend control tags, raw output (`{!! !!}`), and whitespace-control markers (`{{-`, `-}}`) are rejected with a syntax error that names the line and column; a spaced minus such as `{{ -x }}` is ordinary arithmetic. Top-level data keys beginning with `__sword_` are reserved and dropped before rendering, except `__sword_csrf`, which supplies the `@csrf` token when rendering without Laravel. `<x-` and `</x-` start a component tag only when followed by a letter, so text such as `a<x-1` in inline scripts stays literal. Template whitespace is retained except that a newline immediately following a directive or component tag is consumed. Sword maps backend failures to the original template and reports nested call sites when available. Parser syntax errors include the original line and column. Only trusted template authors should supply templates while the expression contract and parser are still under development.
## Conditional attributes, classes and styles
`@checked(cond)`, `@selected(cond)`, `@disabled(cond)`, `@readonly(cond)` and `@required(cond)` output their attribute name when the Sword condition is truthy. `@class([...])` and `@style([...])` render a whole attribute from a list of quoted literals, each optionally followed by `=> condition`; an item is kept when it has no condition or its condition is truthy. Values are HTML-escaped, a style that does not end in `;` gets one, and styles are separated by a space. An empty list renders `class=""`. Conditions use the expression contract; function calls are rejected. Literals must not contain backslashes. Without an argument list these names are ordinary text, so `me@required.dev` is unchanged. Escaping of special characters in literals is covered by tests.
## Switch
```html
@switch(order.status)
@case('new')
@case('pending')
Waiting
@break
@case('shipped')
On its way
@break
@default
Unknown
@endswitch
```
A matching case runs and execution falls through into the following cases until `@break`; `@break(condition)` leaves the switch only when the condition is truthy. The subject is evaluated once and each `@case` is compared with Sword's `==`, so `1` does not match `"1"`; a missing value is an error. `@default` runs when reached, so it must be the last case. Only whitespace may appear between `@switch(...)` and the first `@case`, and that whitespace is not rendered. `@case`, `@default` and a switch-level `@break` must be directly inside the switch, not inside another directive; a `@break` inside a `@foreach` within a case ends that loop. `@continue` inside a switch continues the enclosing loop, whereas PHP treats `continue` in a switch as `break`; it is an error outside a loop. Without an argument list `@switch` and `@case` are text, and `@default` or `@endswitch` glued to a word and followed by an identifier character or dot (`me@default.org`) stay text.