1Rex::Template(3)      User Contributed Perl Documentation     Rex::Template(3)
2
3
4

NAME

6       Rex::Template - simple template engine
7

SYNOPSIS

9        use Rex::Template;
10
11        my $template = Rex::Template->new;
12
13        print $template->parse($content, \%template_vars);
14        print $template->parse($content, @template_vars);
15

DESCRIPTION

17       This is a simple template engine for configuration files. It is
18       included mostly for backwards compatibility, and it is recommended to
19       use Rex::Template::NG instead (for better control of chomping new
20       lines, and better diagnostics if things go wrong).
21
22   SYNTAX
23       The following syntax is recognized:
24
25       ·   anything between "<%" and "%>" markers are considered as a template
26           directive, which is treated as Perl code
27
28       ·   if the opening marker is followed by an equal sign ("<%=") or a
29           plus sign ("<%+"), then the directive is replaced with the value it
30           evaluates to
31
32       ·   if the closing marker is prefixed with a minus sign ("-%>"), then
33           any trailing newlines are chomped for that directive
34
35       The built-in template support is intentionally kept basic and simple.
36       For anything more sophisticated, please use your favorite template
37       engine.
38
39   EXAMPLES
40       Plain text is unchanged:
41
42        my $result = $template->parse( 'one two three', {} );
43
44        # $result is 'one two three'
45
46       Variable interpolation:
47
48        my $result = template->parse( 'Hello, this is <%= $::name %>', { name => 'foo' } ); # original format
49        my $result = template->parse( 'Hello, this is <%+ $::name %>', { name => 'foo' } ); # alternative format with + sign
50        my $result = template->parse( 'Hello, this is <%= $name %>',   { name => 'foo' } ); # local variables
51        my $result = template->parse( 'Hello, this is <%= $name %>',     name => 'foo'   ); # array of variables, instead of hashref
52
53        # $result is 'Hello, this is foo' for all cases above
54
55       Simple evaluation:
56
57        my $result = $template->parse( '<%= join("/", @{$elements} ) %>', elements => [qw(one two three)] );
58        # $result is 'one/two/three'
59
60       Embedded code blocks:
61
62        my $content = '<% if ($logged_in) { %>
63        Logged in!
64        <% } else { %>
65        Logged out!
66        <% } %>';
67
68        my $result = $template->parse( $content, logged_in => 1 );
69
70        # $result is "\nLogged in!\n"
71

DIAGNOSTICS

73       Not much, mainly due to the internal approach of the module.
74
75       If there was a problem, it prints an "INFO" level "syntax error at
76       ...", followed by a "WARN" about "It seems that there was an error
77       processing the template because the result is empty.", and finally
78       "Error processing template at ...".
79
80       The beginning of the reported syntax error might give some clue where
81       the error happened in the template, but that's it.
82
83       Use Rex::Template::NG instead for better diagnostics.
84

CONFIGURATION AND ENVIRONMENT

86       If $Rex::Template::BE_LOCAL is set to a true value, then local template
87       variables are supported instead of only global ones ($foo vs $::foo).
88       The default value is 1 since Rex-0.41. It can be disabled with the
89       no_local_template_vars feature flag.
90
91       If $Rex::Template::DO_CHOMP is set to a true value, then any trailing
92       new line character resulting from template directives are chomped.
93       Defaults to 0.
94
95       This module does not support any environment variables.
96

EXPORTED FUNCTIONS

98   parse($content, $variables)
99       Parse $content as a template, using $variables hash reference to pass
100       name-value pairs of variables to make them available for the template
101       function.
102
103       Alternatively, the variables may be passed as an array instead of a
104       hash reference.
105
106   is_defined($variable, $default_value)
107       This function will check if $variable is defined. If yes, it will
108       return the value of $variable, otherwise it will return $default_value.
109
110       You can use this function inside your templates, for example:
111
112        ServerTokens <%= is_defined( $::server_tokens, 'Prod' ) %>
113

DEPENDENCIES

INCOMPATIBILITIES

BUGS AND LIMITATIONS

117       It might not be able to chomp new line characters resulting from
118       templates in every case.
119
120       It can't report useful diagnostic messages upon errors.
121
122       Use Rex::Template::NG instead.
123
124
125
126perl v5.32.1                      2021-03-06                  Rex::Template(3)
Impressum