Layouts and Partials¶
Template::HAML supports rendering templates from disk, composing them into
layouts, and re-using fragments via partials. These features build on a
small instance API on HAML:
1 2 3 4 | |
Search paths¶
HAML.new(:@search-paths) configures the directories that are searched
when resolving a template by name. The list is ordered: the first match
wins.
1 | |
A single string is accepted as a convenience for the common one-path case:
1 | |
When no search paths are given the current working directory is used.
File rendering¶
1 | |
The template name is resolved against every search path, trying the following filename variants in order:
- exact name (
home/index) <name>.haml<name>.html.haml- partial form:
<dir>/_<basename>(and its.haml/.html.hamlvariants)
$haml.render(:file<...>) looks up and renders one file. $haml.render(:src<...>)
still works for inline source. Absolute paths are honored as-is.
A missing template raises X::HAML::TemplateNotFound, which carries the
attempted name and the configured search paths.
Layouts and yield¶
A layout is a template that contains = yield somewhere in its body.
When you call render(:file, :layout), the inner template is rendered
first; its output is then substituted for yield in the layout.
app/views/layouts/app.html.haml:
1 2 3 | |
app/views/home/index.html.haml:
1 | |
1 | |
renders:
1 2 3 4 5 6 | |
The same call works with the compiled cache via render-file-cached:
1 | |
Both the inner template and the layout are compiled, cached on disk, and memoized in-process. See the API reference for details.
Named content blocks¶
Inner templates can fill named slots in a layout with content-for.
The layout retrieves them via yield(:name<...>):
Inner:
1 2 | |
Layout:
1 2 3 4 5 6 | |
yield and content-for are exported by Template::HAML::Helpers and are
visible to every embedded expression. yield returns a SafeString, so
its result is not double-escaped by the renderer.
Partials¶
A partial is a fragment that is rendered inline from another template.
By convention partial filenames begin with an underscore (_header.haml);
the leading underscore is added automatically by the lookup logic, so the
call site just names the partial:
_header.html.haml:
1 | |
In a parent template:
1 2 | |
Locals¶
Pass per-partial locals through :locals:
1 | |
Inside _greeting.haml:
1 | |
Collections¶
:collection renders the partial once per element. The element is
exposed under the name given by :as (defaults to item):
1 2 | |
Inside _row.haml:
1 | |
Recursion limit¶
Partials may recurse, but the depth is bounded by the rendering context
(default 100). Exceeding the limit raises
X::HAML::PartialDepthExceeded.
Compilation cache¶
Templates loaded by compile-file (used internally by :file and
:partial lookups) are cached per HAML instance, keyed on absolute
path and mtime. Edits to a template are picked up automatically on the
next render.
1 2 3 | |