NAME

Text::ZTemplate - Mustache-like templates with Data::ZPath

SYNOPSIS

use Text::ZTemplate;

my $template = Text::ZTemplate->new(
  string => q{<h1>{{ product/name }}</h1>},
  escape => 'html',
);

my $out = $template->process({
  product => { name => q{A & B} },
});

DESCRIPTION

Text::ZTemplate is a small template engine using Data::ZPath expressions inside {{ ... }} tags.

It supports:

  • Substitutions: {{ expression }}

  • Blocks/loops/tests: {{# expression }} ... {{/ expression }} or {{# expression }} ... {{/}}

  • Per-expression escaping override: {{ expression :: html }} or {{ expression :: raw }}

  • Includes: {{ path/to/include.tmpl }}> (can be disabled with includes => 0)

ZPath expressions are compiled once at template construction and cached in the template object for reuse across calls to process.

METHODS

new( string => $template, escape => $mode, includes => $bool )

new( file => $path, escape => $mode, includes => $bool )

Create a compiled template from a UTF-8 string or file.

$mode defaults to html. Valid values are html and raw.

$bool defaults to true. Set includes => 0 to disable support for {{ ... }}> tags.

process( $data )

Apply the template to $data and return the rendered string.

Include paths are resolved relative to the file that contains that include tag.

ESCAPING

Default escaping is controlled by new(..., escape => ...).

Individual tags can override the default:

{{ product/name :: html }}
{{ product/name :: raw }}

SEE ALSO

Data::ZPath, https://zpath.me/#ztemplate

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2026 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.