1Template::Parser(3) User Contributed Perl Documentation Template::Parser(3)
2
3
4
6 Template::Parser - LALR(1) parser for compiling template documents
7
9 use Template::Parser;
10
11 $parser = Template::Parser->new(\%config);
12 $template = $parser->parse($text)
13 || die $parser->error(), "\n";
14
16 The "Template::Parser" module implements a LALR(1) parser and
17 associated methods for parsing template documents into Perl code.
18
20 new(\%params)
21 The "new()" constructor creates and returns a reference to a new
22 "Template::Parser" object.
23
24 A reference to a hash may be supplied as a parameter to provide
25 configuration values. See "CONFIGURATION OPTIONS" below for a summary
26 of these options and Template::Manual::Config for full details.
27
28 my $parser = Template::Parser->new({
29 START_TAG => quotemeta('<+'),
30 END_TAG => quotemeta('+>'),
31 });
32
33 parse($text)
34 The "parse()" method parses the text passed in the first parameter and
35 returns a reference to a hash array of data defining the compiled
36 representation of the template text, suitable for passing to the
37 Template::Document new() constructor method. On error, undef is
38 returned.
39
40 $data = $parser->parse($text)
41 || die $parser->error();
42
43 The $data hash reference returned contains a "BLOCK" item containing
44 the compiled Perl code for the template, a "DEFBLOCKS" item containing
45 a reference to a hash array of sub-template "BLOCK"s defined within in
46 the template, and a "METADATA" item containing a reference to a hash
47 array of metadata values defined in "META" tags.
48
50 The "Template::Parser" module accepts the following configuration
51 options. Please see Template::Manual::Config for further details on
52 each option.
53
54 START_TAG, END_TAG
55 The START_TAG and END_TAG options are used to specify character
56 sequences or regular expressions that mark the start and end of a
57 template directive.
58
59 my $parser = Template::Parser->new({
60 START_TAG => quotemeta('<+'),
61 END_TAG => quotemeta('+>'),
62 });
63
64 TAG_STYLE
65 The TAG_STYLE option can be used to set both START_TAG and END_TAG
66 according to pre-defined tag styles.
67
68 my $parser = Template::Parser->new({
69 TAG_STYLE => 'star', # [* ... *]
70 });
71
72 PRE_CHOMP, POST_CHOMP
73 The PRE_CHOMP and POST_CHOMP can be set to remove any whitespace before
74 or after a directive tag, respectively.
75
76 my $parser = Template::Parser-E<gt>new({
77 PRE_CHOMP => 1,
78 POST_CHOMP => 1,
79 });
80
81 INTERPOLATE
82 The INTERPOLATE flag can be set to allow variables to be embedded in
83 plain text blocks.
84
85 my $parser = Template::Parser->new({
86 INTERPOLATE => 1,
87 });
88
89 Variables should be prefixed by a "$" to identify them, using curly
90 braces to explicitly scope the variable name where necessary.
91
92 Hello ${name},
93
94 The day today is ${day.today}.
95
96 ANYCASE
97 The ANYCASE option can be set to allow directive keywords to be
98 specified in any case.
99
100 # with ANYCASE set to 1
101 [% INCLUDE foobar %] # OK
102 [% include foobar %] # OK
103 [% include = 10 %] # ERROR, 'include' is a reserved word
104
105 GRAMMAR
106 The GRAMMAR configuration item can be used to specify an alternate
107 grammar for the parser. This allows a modified or entirely new template
108 language to be constructed and used by the Template Toolkit.
109
110 use MyOrg::Template::Grammar;
111
112 my $parser = Template::Parser->new({
113 GRAMMAR = MyOrg::Template::Grammar->new();
114 });
115
116 By default, an instance of the default Template::Grammar will be
117 created and used automatically if a "GRAMMAR" item isn't specified.
118
119 DEBUG
120 The DEBUG option can be used to enable various debugging features of
121 the "Template::Parser" module.
122
123 use Template::Constants qw( :debug );
124
125 my $template = Template->new({
126 DEBUG => DEBUG_PARSER | DEBUG_DIRS,
127 });
128
130 Andy Wardley <abw@wardley.org> <http://wardley.org/>
131
133 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
134
135 This module is free software; you can redistribute it and/or modify it
136 under the same terms as Perl itself.
137
138 The main parsing loop of the "Template::Parser" module was derived from
139 a standalone parser generated by version 0.16 of the "Parse::Yapp"
140 module. The following copyright notice appears in the "Parse::Yapp"
141 documentation.
142
143 The Parse::Yapp module and its related modules and shell
144 scripts are copyright (c) 1998 Francois Desarmenien,
145 France. All rights reserved.
146
147 You may use and distribute them under the terms of either
148 the GNU General Public License or the Artistic License, as
149 specified in the Perl README file.
150
152 Template, Template::Grammar, Template::Directive
153
154
155
156perl v5.32.0 2020-07-28 Template::Parser(3)