Render context¶
HAML.render accepts a :context named argument — a Raku object whose methods
become reachable inside templates as bare identifiers in expression
position. This is the "view context" of frameworks like Rails: a place to put
template-facing helpers without polluting global scope.
1 2 3 4 5 6 7 8 9 10 11 12 | |
renders:
1 2 | |
Bare identifiers¶
A "bare identifier" is an expression that is exactly one name — letters, digits, underscore, hyphen — with no parentheses, sigils, or operators. Examples:
| Expression | Bare identifier? |
|---|---|
title |
yes |
user-count |
yes |
$title |
no — has sigil |
title() |
no — has parens |
title.uc |
no — has . |
A bare identifier in =, !=, &=, ~, or in a control-flow condition
(if, unless, elsif, while, repeat, given) resolves through:
- Locals — if
%locals{name}exists, use it. - Context method — otherwise if
context.^can(name)is true, callcontext.name. - Fallback to eval — otherwise the expression is handed to the normal
Raku evaluator. If the name isn't a valid Raku identifier in scope, you'll
get
X::HAML::Eval.
1 2 3 4 | |
Default context¶
If you don't pass :context, the renderer constructs a
Template::HAML::ViewContext for you. It composes the
Template::HAML::HelpersRole role, so every built-in helper (surround,
escape-once, list-of, find-and-preserve, html-safe, capture-haml,
yield, content-for, tab-up, tab-down, haml-concat) is reachable as
a method on the context.
Extending the context¶
Two composition points are supported.
Mix in the helper role into your own class:
1 2 3 4 5 6 7 8 | |
The role brings the built-in helpers along, so you can add your own methods
without losing surround, list-of, etc.
Subclass ViewContext:
1 2 3 4 5 | |
Either approach works; the role form is slightly more flexible because you can compose other roles alongside it:
1 2 3 4 | |
Bare identifiers (hello, footer, surround, …) resolve against every
composed role.
Locals still win¶
When both a local and a context method have the same name, the local wins:
1 2 3 4 5 6 7 8 9 10 | |
This is the same precedence used by Rails-style view contexts and keeps per-render values from being silently shadowed by a stray helper method.