Rex::Template(3pm)
π Rex::Template π
okay bestie, so this is Rex::Template, a page from the User Contributed Perl Documentation section of the manual (yep, the 3pm girlies π
). Let's get into it.
β¨ NAME β¨
Rex::Template β a simple template engine π
πΈ SYNOPSIS πΈ
use Rex::Template;
my $template = Rex::Template->new;
print $template->parse($content, \%template_vars);
print $template->parse($content, @template_vars);
π¦ DESCRIPTION π¦
So this is a simple template engine for configuration files β¨. It's included mostly for backwards compatibility, and lowkey the move is to use Rex::Template::NG instead β it gives you better control of chomping new lines and better diagnostics if things go wrong. No cap.
π₯ SYNTAX π₯
Here's what it actually recognizes:
- anything between
<%and%>markers counts as a template directive, and it gets treated as Perl code π» - if the opening marker is followed by an equal sign (
<%=) or a plus sign (<%+), then the directive is replaced with the value it evaluates to π«Ά - if the closing marker is prefixed with a minus sign (
-%>), then any trailing newlines are chomped for that directive βοΈ
The built-in template support is intentionally kept basic and simple. For anything more sophisticated, bestie, please use your favorite template engine π.
π EXAMPLES π
Plain text is unchanged, iconic behavior, it stays in its lane:
my $result = $template->parse( 'one two three', {} );
# $result is 'one two three'
Variable interpolation π«:
my $result = template->parse( 'Hello, this is <%= $::name %>', { name => 'foo' } ); # original format
my $result = template->parse( 'Hello, this is <%+ $::name %>', { name => 'foo' } ); # alternative format with + sign
my $result = template->parse( 'Hello, this is <%= $name %>', { name => 'foo' } ); # local variables
my $result = template->parse( 'Hello, this is <%= $name %>', name => 'foo' ); # array of variables, instead of hashref
# $result is 'Hello, this is foo' for all cases above
Simple evaluation, she's doing math and serving looks:
my $result = $template->parse( '<%= join("/", @{$elements} ) %>', elements => [qw(one two three)] );
# $result is 'one/two/three'
Embedded code blocks, main character energy fr:
my $content = '<% if ($logged_in) { %>
Logged in!
<% } else { %>
Logged out!
<% } %>';
my $result = $template->parse( $content, logged_in => 1 );
# $result is "\nLogged in!\n"
π DIAGNOSTICS π
Not much, bestie. Mainly due to the internal approach of the module π.
If there was a problem, it prints an "INFO" level "syntax error at ...", followed by a "WARN" about "It seems that there was an error processing the template because the result is empty.", and finally "Error processing template at ...".
The beginning of the reported syntax error might give some clue where the error happened in the template, but that's it π« .
Use Rex::Template::NG instead for better diagnostics. periodt.
π CONFIGURATION AND ENVIRONMENT π
- If
$Rex::Template::BE_LOCALis set to a true value, then local template variables are supported instead of only global ones ($foovs$::foo). The default value is1since Rex-0.41. It can be disabled with theno_local_template_varsfeature flag β¨. - If
$Rex::Template::DO_CHOMPis set to a true value, then any trailing new line character resulting from template directives are chomped. Defaults to0π.
This module does not support any environment variables. nope, not a single one π
π EXPORTED FUNCTIONS π
π parse($content, $variables)
Parse $content as a template, using $variables hash reference to pass name-value pairs of variables to make them available for the template function π«Ά.
Alternatively, the variables may be passed as an array instead of a hash reference. flexible queen β¨.
π is_defined($variable, $default_value)
This function will check if $variable is defined. If yes, it will return the value of $variable, otherwise it will return $default_value π.
You can use this function inside your templates, for example:
ServerTokens <%= is_defined( $::server_tokens, 'Prod' ) %>
π§ DEPENDENCIES π§
(nothing listed here, bestie β she's self-sufficient π )
π INCOMPATIBILITIES π
(also empty. no drama here π)
β BUGS AND LIMITATIONS β
- It might not be able to chomp new line characters resulting from templates in every case π
- It can't report useful diagnostic messages upon errors π
Use Rex::Template::NG instead. fr fr.
and that's the whole thing bestie, slay πβ¨π¦
perl v5.40.1 Β· 2025-10-03 Β· that's a wrap on Rex::Template(3pm), go forth and template responsibly π
π¦ where this page comes from
- ubuntu resolute: rex 1.16.1-1 Β· Apache-2.0
- ubuntu stonking: rex 1.16.1-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms