NAME
Gears::Template - Abstract template processing interface
SYNOPSIS
package My::Gears::Template;
use v5.40;
use Mooish::Base -standard;
extends 'Gears::Template';
sub _render_template ($self, $template_content, $vars)
{
my $result = $template_content;
# Simple variable substitution
foreach my $key (keys %$vars) {
my $value = $vars->{$key};
$result =~ s/\{\{$key\}\}/$value/g;
}
return $result;
}
# In your code
use My::Gears::Template;
my $template = My::Gears::Template->new(
paths => ['templates']
);
# Process a file template
my $output = $template->process('page.tmpl', {title => 'Hello'});
# Process a scalar reference
my $output = $template->process(\'Hello {{name}}', {name => 'World'});
DESCRIPTION
Gears::Template is an abstract base class for template processing functionality. It provides file handling, template discovery, and encoding support, but leaves the actual template processing to subclasses. This allows different template engines to be used with a consistent interface.
The template processor can work with templates from multiple sources: files (found via search paths), scalar references, or file handles. It handles encoding automatically and provides a simple API for template processing.
EXTENDING
This template processor is abstract by design. A subclass must be created that implements the _render_template method to define how templates are actually processed.
Here is how a minimal working template subclass could be implemented:
package My::Gears::Template;
use v5.40;
use Mooish::Base -standard;
extends 'Gears::Template';
sub _render_template ($self, $template_content, $vars)
{
# Simple variable substitution
foreach my $key (keys %$vars) {
$template_content =~ s/\Q{{$key}}\E/$vars->{$key}/g;
}
return $template_content;
}
For a more advanced template engine using Template::Toolkit:
package My::Gears::Template::TT;
use v5.40;
use Mooish::Base -standard;
use Template;
extends 'Gears::Template';
has param 'tt' => (
isa => InstanceOf ['Template'],
default => sub { Template->new },
);
sub _render_template ($self, $template_content, $vars)
{
my $output;
Gears::X::Template->raise($self->tt->error)
unless $self->tt->process(\$template_content, $vars, \$output);
return $output;
}
INTERFACE
Attributes
encoding
A string specifying the encoding to use when reading template files. Defaults to 'UTF-8'.
Available in constructor
paths
An array reference of directory paths where template files should be searched for. Defaults to an empty array. Templates are searched in the order paths are specified, and the first matching file is used.
Available in constructor
Methods
new
$object = $class->new(%args)
A standard Mooish constructor. Consult "Attributes" section to learn what keys can be passed in %args.
process
$output = $template->process($template, $vars = {})
Processes a template with the provided variables and returns the result. The $template argument can be:
A string - interpreted as a filename to be found via
find_templateA SCALAR reference - the template content itself
A GLOB reference or IO::Handle - an open file handle to read from
The optional $vars hash reference contains variables to be used during template processing.
Returns the processed template as a string.