HtmlTemplate¶
An HTML-aware template engine with contextual auto-escaping.
HtmlTemplate uses the same template syntax as Template but automatically
escapes variable output based on HTML context. A variable inside a <p> tag
gets HTML entity escaping; inside an href attribute it gets URL escaping
and dangerous scheme filtering; inside an onclick attribute it gets
JavaScript string escaping; and so on.
Parse-time validation rejects templates with variables in structurally
invalid positions (inside tag names, unquoted attribute values, etc.) and
verifies that if/else branches and loops preserve HTML context
consistency.
To bypass auto-escaping for trusted content, use TemplateValue.unescaped
or TemplateValues.unescaped. Plain Template ignores the escaping
annotations entirely — they only take effect in HtmlTemplate.
Constructors¶
parse¶
Parse an HTML template from a string. Raises an error if the template has syntax errors or if variables appear in invalid HTML positions (tag names, unquoted attributes, etc.).
Parameters¶
- source: String val
- ctx: TemplateContext val = call
Returns¶
- HtmlTemplate val^ ?
from_file¶
Parse an HTML template from a file. Raises an error if the file cannot be read, the template has syntax errors, or variables appear in invalid HTML positions.
Parameters¶
- path: FilePath val
- ctx: TemplateContext val = call
Returns¶
- HtmlTemplate val^ ?
Public Functions¶
render¶
Render the template with the given values. Variable output is
automatically escaped based on HTML context unless the value was
created with TemplateValue.unescaped.
Parameters¶
- values: TemplateValues box
Returns¶
- String val ?
render_to¶
Walk the template and drive the given sink with alternating literal and
dynamic_value calls. Dynamic values are already escaped based on HTML
context — the sink receives final, safe strings. See TemplateSink for
the interleaving guarantee.
Parameters¶
- sink: TemplateSink ref
- values: TemplateValues box
Returns¶
- None val ?
render_split¶
Render the template and return the static literal segments and dynamic
value segments as separate arrays. Dynamic values are already escaped
based on HTML context. For N dynamic insertions, the statics array has
N+1 entries. Concatenating statics(0) + dynamics(0) + statics(1) +
dynamics(1) + ... + statics(N) produces the same result as render().
fun box render_split(
values: TemplateValues box)
: (Array[String val] val , Array[String val] val) ?
Parameters¶
- values: TemplateValues box