std/template/z

Standard Library documentation

Pure ZuzuScript template engine.

Module

Name
std/template/z
Area
Standard Library
Source
modules/std/template/z.zzm

NAME

std/template/z - Pure ZuzuScript template engine.

SYNOPSIS

  from std/template/z import ZTemplate;

  let inline := new ZTemplate(
    string: "Hello {{ user/name }}!",
  );
  say( inline.process( { user: { name: "Ada" } } ) );

  let file_tmpl := new ZTemplate(
    file: "templates/page.zt",
    escape: "html",
  );
  say( file_tmpl.process( data ) );

IMPLEMENTATION SUPPORT

This module is supported by zuzu.pl, zuzu-rust, and zuzu-js on Node and Electron. It is partially supported by zuzu-js in the browser: complex, HTTP/JSON/ZPath pipeline, self-contained, and trait-object rendering coverage passes, but filesystem-backed template loading and database row rendering coverage are unsupported.

DESCRIPTION

std/template/z is a pure ZuzuScript template renderer.

The module compiles template text into a cached parse tree and renders it against data using std/path/z expressions.

EXPORTS

Classes

  • ZTemplate({ string?: String, file?, escape?: String, includes?: Boolean })

    Constructs a template from inline text or a file path. Returns: ZTemplate.

    • template.process(data)

      Parameters: data is the model used for path lookups. Returns: String. Renders the template.

TAG FORMS

  • {{ expr }}

    Render expression output using template default escaping.

  • {{ expr :: raw }}, {{ expr :: html }}

    Render expression output using per-tag escape override.

  • {{# expr }} ... {{/expr}}

    Block form. Each truthy match renders child nodes.

  • {{ include.zt }}>

    Include another template file. Relative include paths resolve from the current template's file directory.

ESCAPING

Default escape mode is html. Supported escape modes are html and raw.

html escapes &, <, E<gt>, double quotes, and single quotes.

INCLUDE RULES

  • Include processing is enabled by default.
  • Pass includes: false to disable include tags.
  • Relative includes require a file-backed template source.
  • Circular include chains throw a deterministic error.

KNOWN DIFFERENCES

This module is implemented on top of std/path/z. Parser/rendering deltas across runtimes are covered in ztests and should be treated as implementation bugs unless explicitly documented.

COPYRIGHT AND LICENCE

std/template/z is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.