How it works
When a child template is rendered:- The child executes first — sections are captured into named buffers, the layout name is stored.
- Any output outside
#section/#endsectionin the child is discarded. - The layout executes using the same environment — each
#yielddraws from the captured sections. - If the layout itself extends another layout, the process repeats upward until a root layout is reached.
Templates execute top-down; layouts are assembled inside-out. The innermost child runs first, the outermost layout last.
Directives
| Directive | Used in | Purpose |
|---|---|---|
#yield('name') | Layout | Outputs the named section. Nothing is emitted if the child didn’t define it. |
#yield('name', 'default') | Layout | Outputs a fallback string when the section is absent. |
#extends('layout') | Child page | Declares which layout to inherit. Must be the first directive in the file. |
#section('name') … #endsection | Child page | Defines the content for a named yield slot. |
#parent | Inside #section | Injects the parent layout’s own version of that section before your content. |
Creating a layout
A layout is an ordinary.lex file that uses #yield to mark where child content goes.
views/layouts/base.lex
#yield('title', 'My App') outputs My App if no child defines a title section. #stack('styles') and #stack('scripts') are covered in Stacks.
Extending the layout in a child page
In the child template, call#extends first, then define your sections in any order.
views/pages/home.lex
Dot notation maps to directory paths:
layouts.base resolves to views/layouts/base.lex.Full working example
Create the layout file
Define your HTML skeleton with
#yield slots at views/layouts/app.lex:views/layouts/app.lex
Create the child page
Extend the layout and fill in the sections at
views/pages/dashboard.lex:views/pages/dashboard.lex
The #parent directive
Use #parent inside a #section block to prepend the parent layout’s own version of that section before your additions. This lets you extend a section rather than replace it entirely.
- Layout
- Mid-level layout
- Child page
- Rendered output
views/layouts/base.lex
Multi-level inheritance
A layout can itself extend another layout. The rendering chain walks upward until it reaches a root layout with no#extends.
views/layouts/base.lex
views/layouts/app.lex
views/pages/about.lex
If two layouts extend each other in a cycle (A → B → A), Lex detects the loop and throws
TemplateRuntimeException immediately — no infinite recursion.Default section content
Pass a second argument to#yield to display fallback text when no child defines the section: