Rand Stats

Template::Nest::Fast

zef:andinus

Table of Contents

  1. Documentation
    1. Options
    2. Methods
    3. Example
      1. Simple template hash
      2. Array of template hash
      3. Array to template variable
  2. News
    1. v0.2.8 - 2023-06-27
    2. v0.2.6 - 2023-05-29
    3. v0.2.5 - 2023-05-25
    4. v0.2.4 - 2023-05-22
    5. v0.2.3 - 2023-05-14
    6. v0.2.2 - 2023-05-07
    7. v0.2.1 - 2023-05-04
    8. v0.2.0 - 2023-05-02
    9. v0.1.0 - 2023-03-28
  3. See Also

Documentation

Template::Nest::Fast is a high-performance template engine module for Raku, designed to process nested templates quickly and efficiently. This module improves on the original Template::Nest module by caching the index of positions of variables, resulting in significantly faster processing times.

For more details on Template::Nest visit: https://metacpan.org/pod/Template::Nest

Note: This module was created by me as a proof-of-concept to benchmark against Template::Nest::XS. Tom Gracey (virtual.blue) is currently sponsoring for the development of this module. He authored Template::Nest originally in Perl 5.

Note: Template::Nest::XS is the recommended module for use in production. Considerable effort has gone into optimising Template::Nest::XS, and it is now blindingly fast. Make sure to use the latest version (v0.1.9 at the time of writing), as some instabilities have recently been resolved.

As a pure Raku version, you may also find this module (Template::Nest::Fast) useful for development/testing purposes - as e.g. a full stack trace can be obtained from it (unlike the XS version which is more of a black box). There may be other circumstances where this module is useful. However be aware it is approx 25 times slower than the XS version.

It is not recommended to use the original pure Raku version of Template::Nest. This module was a line by line rewrite of the Perl 5 module - which unfortunately turned out to be far too slow to be practical.

Options

Compatibility Progress:

[X] comment_delims
[X] defaults
[X] defaults_namespace_char
[X] die_on_bad_params
[ ] escape_char             - Won't be implemented
[X] fixed_indent
[X] name_label
[X] show_labels
[X] template_dir
[X] template_ext
[X] token_delims

Methods

Example

Templates: templates/00-simple-page.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Simple Page</title>
  </head>
  <body>
    <p>A fairly simple page to test the performance of Template::Nest.</p>
    <p><!--% variable %--></p>
    <!--% simple_component %-->
  </body>
</html>

templates/01-simple-component.html:

<p><!--% variable %--></p>

Simple template hash

This is a simple example that injects a variable in a template. We use another template as a component as well.

use Template::Nest::Fast;

# Create a nest object.
my $nest = Template::Nest::Fast.new( template-dir => 'templates/'.IO );

# Declare template structure.
my %simple-page = %(
    TEMPLATE => '00-simple-page',
    variable => 'Simple Variable',
    simple_component => %(
        TEMPLATE => '01-simple-component',
        variable => 'Simple Variable in Simple Component'
    )
);

# Render the page.
put $nest.render(%simple-page);

Output:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Simple Page</title>
  </head>
  <body>
    <p>A fairly simple page to test the performance of Template::Nest.</p>
    <p>Simple Variable</p>
    <p>Simple Variable in Simple Component</p>
  </body>
</html>

Array of template hash

Array of template hash can be passed to render too.

$nest.render(
    [
        %( TEMPLATE => '01-simple-component',  variable => 'This is a variable' ),
        %( TEMPLATE => '01-simple-component',  variable => 'This is another variable' )
    ]
)

Output:

<p>This is a variable</p><p>This is another variable</p>

Array to template variable

Template variable can be a string, another template hash or an array too. The array itself can contain template hash, string or nested array.

my %simple-page-arrays = %(
    TEMPLATE => '00-simple-page',
    variable => 'Simple Variable',
    simple_component => [
                         # Hash passed.
                         %(
                             TEMPLATE => '01-simple-component',
                             variable => 'Simple Variable in Simple Component'
                         ),
                         # Can pass string as well.
                         "<strong>Another test</strong>",
                         # Or another level of nesting.
                         [
                             %(
                                 TEMPLATE => '01-simple-component',
                                 variable => 'Simple Variable in Simple Component'
                             ),
                             "<strong>Another nested test 2</strong>"
                         ]
                     ]
);

Output:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Simple Page</title>
  </head>
  <body>
    <p>A fairly simple page to test the performance of Template::Nest.</p>
    <p>Simple Variable</p>
    <p>Simple Variable in Simple Component</p><strong>Another test</strong><p>Simple Variable in Simple Component</p><strong>Another nested test 2</strong>
  </body>
</html>

News

v0.2.8 - 2023-06-27

v0.2.6 - 2023-05-29

v0.2.5 - 2023-05-25

v0.2.4 - 2023-05-22

v0.2.3 - 2023-05-14

v0.2.2 - 2023-05-07

v0.2.1 - 2023-05-04

v0.2.0 - 2023-05-02

v0.1.0 - 2023-03-28

See Also