use Template::TT3::Tokens; # create a token list my $tokens = Template::TT3::Tokens->new; # push tokens onto the list, specifying token text and position. # e.g. to tokenise: "Hello [% name %]" $tokens->text_token('Hello ', 0); $tokens->tag_start_token('[%', 6); $tokens->whitespace_token(' ', 8); $tokens->word_token('name', 9); $tokens->whitespace_token(' ', 13); $tokens->tag_end_token('%]', 14);
This module implements an object which is used to construct a list of tokens scanned from the source text of a template.
Each token is represented as a Template::TT3::Element object (or subclass thereof). These are loaded on demand by the Template::TT3::Elements factory module.
The token methods are also created on demand. For example, the text_token()
method doesn't exist until you first call it. At this point, the
AUTOLOAD
method will be invoked which will in turn call the
token_method()
method.
This then asks the
Template::TT3::Elements
factory to load whatever
module corresponds to the text
element type. If it successfully loads
a module then the
token_method()
method constructs a new text_token()
method that delegates to the element class. From that point on, the
text_token()
method is defined and can be called directly.
The end result is that you can call any xxx_token()
method and have it
automatically load the element corresponding to the xxx
prefix. This
makes it possible to define any number of custom element types without
having to worry about loading them all in advance on the off-chance that
they may be used.
The elements
option can be used to define the elements that the
Template::TT3::Tokens
object should recognise.
The default value is Template::TT3::Elements
. If you want to use a
different element factory module then you can specify it by name:
my $tokens = Template::TT3::Tokens->new( elements => 'My::Elements', );
Or you can provide a reference to an element factory object:
my $tokens = Template::TT3::Tokens->new( elements => My::Elements->new(), );
If you want to use the default Template::TT3::Elements
factory module
but with some additional elements of your own then you can specify them as
a hash reference.
my $tokens = Template::TT3::Tokens->new( elements => { foo => 'My::Element::Foo', bar => 'My::Element::Bar', }, );
This is short-hand convention for the following code:
my $tokens = Template::TT3::Tokens->new( elements => Template::TT3::Elements->new( elements => { foo => 'My::Element::Foo', bar => 'My::Element::Bar', }, ), );
If you want to specify a custom element module and some custom elements then provide them as items in an array reference, like so:
my $tokens = Template::TT3::Tokens->new( elements => [ 'My::Elements' => { foo => 'My::Element::Foo', bar => 'My::Element::Bar', }, }, );
This has the same effect as the following more explicit code:
my $tokens = Template::TT3::Tokens->new( elements => My::Elements->new( elements => { foo => 'My::Element::Foo', bar => 'My::Element::Bar', }, ), );