### Create Custom Directive with `AbstractDirective` Source: https://context7.com/spiral/stempler/llms.txt Extend `AbstractDirective` to create custom directives. Methods named `render` will be invoked for corresponding `@directive` calls in templates. This example shows how to create `@asset` and `@vite` directives, registering them with `DynamicToPHP`. ```php use Spiral\Stempler\Directive\AbstractDirective; use Spiral\Stempler\Node\Dynamic\Directive; final class AssetDirective extends AbstractDirective { // renders @asset('path/to/file.css') public function renderAsset(Directive $directive): string { $path = trim($directive->body, "'\""); return sprintf('', var_export($path, true)); } // renders @vite('resources/app.js') public function renderVite(Directive $directive): string { return sprintf('', $directive->body); } } // Register with DynamicToPHP finalizer $dynamic = new \Spiral\Stempler\Transform\Finalizer\DynamicToPHP(); $dynamic->addDirective(new AssetDirective()); $builder->addVisitor($dynamic, Builder::STAGE_FINALIZE); // Template: // @asset('css/app.css') → // @vite('resources/app.js') → ``` -------------------------------- ### Conditional and Loop Directives (`@if`, `@foreach`) Source: https://context7.com/spiral/stempler/llms.txt Utilize built-in directives like `@foreach`, `@if`, `@elseif`, and `@else` to render standard PHP control structures. These directives are processed by `ConditionalDirective` and `LoopDirective` and should be registered via `DynamicToPHP` at `STAGE_FINALIZE`. The example also includes a `@json` directive. ```php use Spiral\Stempler\Loader\StringLoader; $loader = new StringLoader(); $loader->set('list', ' @foreach($items as $item) @if($item["active"])
  • {{ $item["label"] }}
  • @elseif($item["hidden"]) {{-- hidden item, skip --}} @else
  • {{ $item["label"] }}
  • @endif @endforeach @json($config) '); // Compiled to standard PHP foreach / if / endif blocks + json_encode $result = $builder->compile('list'); echo $result->getContent(); ``` -------------------------------- ### Dynamic and Raw Output with `{{ }}` and `{!! !!}` Source: https://context7.com/spiral/stempler/llms.txt Control output escaping using `{{ $expr }}` for context-aware XSS-escaped output (defaults to `htmlspecialchars`) and `{!! $expr !!}` for raw, unescaped output. The example shows both in HTML paragraphs and within a ` '); // Compiled output: //

    Escaped:

    //

    Raw HTML:

    // // Note: {{ }} inside