Skip to content

HtmlTemplate

[Source]

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.

class val HtmlTemplate

Constructors

parse

[Source]

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.).

new val parse(
  source: String val,
  ctx: TemplateContext val = call)
: HtmlTemplate val^ ?

Parameters

Returns


from_file

[Source]

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.

new val from_file(
  path: FilePath val,
  ctx: TemplateContext val = call)
: HtmlTemplate val^ ?

Parameters

Returns


Public Functions

render

[Source]

Render the template with the given values. Variable output is automatically escaped based on HTML context unless the value was created with TemplateValue.unescaped.

fun box render(
  values: TemplateValues box)
: String val ?

Parameters

Returns


render_to

[Source]

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.

fun box render_to(
  sink: TemplateSink ref,
  values: TemplateValues box)
: None val ?

Parameters

Returns


render_split

[Source]

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

Returns