use Template::TT3::Elements; my $elems = Template::TT3::Element->new( # custom elements elements => { foo => 'Some::Element::Module::Foo', }, # custom element path element_path => [ 'My::Element', 'Template::TT3::Element', ], }; my $text = $elems->create( text => 'Hello World' ); my $num = $elems->create( number => 42 );
This module is a subclass of Template::TT3::Factory for locating, loading and instantiating element modules. These are used to represent the component parts (a.k.a. tokens, expressions, opcodes, nodes, etc) of compiled templates.
It searches for element modules in the following places:
Template::TT3::Element Template::Element TemplateX::TT3::Element TemplateX::Element
For example, creating a text
element returns a
Template::TT3::Element::Text
object.
my $text = Template::TT3::Elements->create( text => 'Hello World' );
The following configuration options are defined in addition to those inherited from the Template::TT3::Factory , Template::TT3::Base , Badger::Factory and Badger::Base base classes.
They should be specified as a list or reference to a hash array of named parameters when the factory object is created.
# either a list of named parameters... my $elements = Template::TT3::Elements->new( element_path => [ 'My::Element', 'Template::TT3::Element' ], ); # ...or a reference to a hash array my $elements = Template::TT3::Elements->new({ element_path => [ 'My::Element', 'Template::TT3::Element' ], });
A reference to a hash array explicitly mapping internal element names to external Perl modules. This can be used to override and/or augment the element modules that the factory would normally be able to locate automatically.
my $elements = Template::TT3::Elements->new( elements => { foo => 'Some::Other::Element::Foo', bar => 'Yet::Another::Element::Bar' }, );
A reference to a list of module namespaces that the factory should search to locate element modules. The default path is defined by the $PATH package variable.
my $elements = Template::TT3::Elements->new( element_path => [ 'My::Element', 'Template::TT3::Element' ], );
A reference to a hash array providing aliases for element names.
my $elements = Template::TT3::Elements->new( element_names => { FOO => 'foo', bar => 'foo', }, );
A reference to a hash array a prefixes that should be expanded in element
types. The default set of prefixes is defined in the
$PREFIXES
package
variable. It includes prefixes like op_
as an alias for operator.
,
and num_
as an alias for operator.number.
.
my $elements = Template::TT3::Elements->new( prefixes => { html_ => 'markup.HTML.', } );
With the html_
prefix defined we can then write:
my $table = $elements->create( html_table => '...', );
This is shorthand for:
my $table = $elements->create( 'markup.HTML.table => '...', );
That would be resolved to the Template::TT3::Element::Markup::HTML::Table
module. Note that this module doesn't actually exist, it's just being used
as an example of the prefix expansion.
The following methods are implemented or automatically added by the Template::TT3::Class::Factory metaprogramming module in addition to those inherited from the Template::TT3::Factory , Template::TT3::Base , Badger::Factory and Badger::Base base classes.
Locates and loads an element module and returns the class name. This is created as an alias to the item() method in Badger::Factory .
Note that the method doesn't automatically create a new element object as most factory modules do. TT uses this module to provide constructor functions (via constructor() ) that create instances of the elements rather than creating element objects directly. See create() for a method that does create element objects.
Method for inspecting or modifying the element types that the factory module manages. This is created as an alias to the items() method in Badger::Factory .
Constructor method which creates a new instance variable of a particular element.
my $text = $elems->create( text => 'Hello World' ); my $list = $elems->create( number => 42 );
Returns a reference to a constructor function which can be used to create
element objects of a particular type, denoted by the first argument, $type
.
my $Text = $elems->constructor('text'); my $text = $Text->('Hello World');
Optional configuration parameters can be specified after the element type. These are forwarded to the constructor() method of the appropriate element class.
Custom initialisation method which performs some additional configuration for this factory module.
This replaces the default method inherited from the
Badger::Factory
base
class. It expand any prefix at the start of $type
that matches an entry in
the $PREFIXES
table, or was specified using the
prefixes
configuration
option.
This replaces the default method inherited from the Badger::Factory base class. Instead of automatically creating an element object when the element() method is called, it instead returns the class name of the module implementing it.
This replaces the default method inherited from the Badger::Factory base class. It throws an error if an element cannot be found.
This replaces the default method inherited from the
Badger::Factory
base
class. It allows type names to contain periods as package separator. e.g.
requesting an element with a type of foo.bar_baz
will resolve to the
Template::TT3::Element::Foo::BarBaz
module (or would if it existed outside
the realm of this example).
This module defines the following package variables. These are declarations that are used by the Badger::Factory base class.
This is the name of the item that the factory module returns. In this case it
is defined as element
.
This defines the module search path for the factory. In this case it is defined as a list of the following values;
Template::TT3::Element Template::Element TemplateX::TT3::Element TemplateX::Element
This defines the default set of prefixes that can be used as shorthand for longer element names. At the time of writing it contains the prefixes listed below. Note however that this is subject to change. Consult the source code for the definitive list (and please consider submitting a documentation patch if you find it to be in error).
Prefix Expanded To -------------------------------- bool_ operator.boolean. boolean_ operator.boolean. cmd_ command. command_ command. con_ construct. construct_ construct. ctr_ control. control_ control. html_ HTML. num_ operator.number. number_ operator.number. op_ operator. operator_ operator. sig_ sigil. sigil_ sigil. txt_ operator.text. text_ operator.text. var_ variable. variable_ variable.
Andy Wardley http://wardley.org/
Copyright (C) 1996-2009 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This module inherits methods from the Template::TT3::Factory , Template::TT3::Base , Badger::Factory , and Badger::Base base classes.
It is constructed using the Template::TT3::Class::Factory class metaprogramming module.
It loads modules and instantiates object that are subclasses of Template::TT3::Element . See Template::TT3::Element::Text and Template::TT3::Element::Block for examples.