NAME
Trickster::Template - Fast template engine for Trickster using Text::Xslate
SYNOPSIS
use Trickster::Template;
# Create template engine
my $template = Trickster::Template->new(
path => ['templates', 'views'],
cache => 1,
layout => 'layouts/main.tx',
);
# Render a template
my $html = $template->render('user/profile.tx', {
user => $user,
title => 'User Profile',
});
# In your Trickster app
$app->get('/profile', sub {
my ($req, $res) = @_;
my $html = $template->render('profile.tx', { name => 'Alice' });
return $res->html($html);
});
DESCRIPTION
Trickster::Template provides a fast, feature-rich template engine using Text::Xslate. It supports layouts, custom functions, and integrates seamlessly with Trickster applications.
FEATURES
Fast template rendering with Text::Xslate
Layout support for consistent page structure
Template caching for production performance
Custom function registration
Default variables across all templates
Multiple template paths
TTerse syntax (Template Toolkit-like)
METHODS
new(%options)
Creates a new template engine instance.
Options:
path - Array ref of template directories (default: ['templates'])
cache - Enable template caching (default: 1)
cache_dir - Cache directory (default: system temp)
layout - Default layout template
syntax - Template syntax (default: 'TTerse')
default_vars - Hash ref of default variables
function - Hash ref of custom functions
render($template, $vars)
Renders a template file with the given variables.
my $html = $template->render('user.tx', { name => 'Alice' });
render_string($template_string, $vars)
Renders a template string directly.
my $html = $template->render_string(
'Hello [% name %]!',
{ name => 'Alice' }
);
set_layout($layout)
Sets the default layout template.
$template->set_layout('layouts/main.tx');
set_default_vars($vars)
Sets default variables available to all templates.
$template->set_default_vars({
app_name => 'My App',
version => '1.0',
});
TEMPLATE SYNTAX
Trickster::Template uses TTerse syntax by default (Template Toolkit-like):
[% # Comments %]
[% # Variables %]
Hello [% name %]!
[% # Conditionals %]
[% IF user.is_admin %]
<p>Admin panel</p>
[% END %]
[% # Loops %]
[% FOR item IN items %]
<li>[% item.name %]</li>
[% END %]
[% # Filters %]
[% text | html %]
[% url | uri %]
[% # Include other templates %]
[% INCLUDE 'header.tx' %]
LAYOUTS
Create a layout template (layouts/main.tx):
<!DOCTYPE html>
<html>
<head>
<title>[% title || 'My App' %]</title>
</head>
<body>
[% content %]
</body>
</html>
Use in your templates:
[% # This content will be wrapped in the layout %]
<h1>Welcome [% user.name %]</h1>
Disable layout for specific renders:
$template->render('page.tx', { no_layout => 1 });
CUSTOM FUNCTIONS
Register custom functions for use in templates:
my $template = Trickster::Template->new(
function => {
format_date => sub {
my $timestamp = shift;
return localtime($timestamp)->strftime('%Y-%m-%d');
},
truncate => sub {
my ($text, $length) = @_;
return substr($text, 0, $length) . '...';
},
},
);
Use in templates:
[% format_date(post.created_at) %]
[% truncate(post.body, 100) %]