NAME
MooseX::Templated - template-based rendering of Moose objects
SYNOPSIS
package Farm::Cow;
use Moose;
with 'MooseX::Templated';
has 'spots' => ( is => 'rw' );
has 'hobbies' => ( is => 'rw', default => sub { ['mooing', 'chewing'] } );
sub make_a_happy_noise { "Mooooooo" }
Specify template:
sub _template { <<'_TT2' }
This cow has [% self.spots %] spots - it likes
[% self.hobbies.join(" and ") %].
[% self.make_a_happy_noise %]!
_TT2
Or as a separate file:
# lib/Farm/Cow.tt
Render the object:
$cow = Farm::Cow->new( spots => '8' );
print $cow->render();
# This cow has 8 spots - it likes
# mooing and chewing.
# Mooooooo!
Provide options (such as default file location):
# lib/Farm/Cow.pm
with 'MooseX::Templated' => {
template_suffix => '.tt2',
template_root => '__LIB__/../root',
};
# now looks for
# root/Farm/Cow.tt2
DESCRIPTION
The MooseX::Templated
role provides the consuming class with a method render()
which allows template-based rendering of the object.
METHODS
The following methods are provided to the consuming class
template_engine
Returns MooseX::Template::Engine which is the templating engine responsible for rendering the template.
render
Finds the template source, performs the rendering, returns the rendered result as a string.
Note: the location of the template source is affected by (optional) arguments and role configuration (see below for details).
TEMPLATE SOURCE
The template engine will search for the template source in a few different locations: files, methods, inline.
Farm::Cow->new()->render()
File system
This will look for a template file that relates to the calling package. With default settings, the above example would look for:
__LIB__/Farm/Cow.tt
Where __LIB__
is the root directory for the modules.
The file path can be affected by configuration options: template_root
, template_suffix
Local method in code
Define a local method within the calling package which returns the template source as a string. With default settings, this will look for the method "_template"
, e.g.
sub Farm::Cow::_template { ... }
The expected method name is affected by configuration option: template_method_stub
.
Inline
Provide the template source directly to the render function (as a reference to the template string).
Farm::Cow->render( \"Cow goes [% self.moo %]!" );
CONFIGURATION
Defaults about how to find your template files / methods can be provided at role composition:
with 'MooseX::Templated' => {
view_class => 'MooseX::Templated::View::TT',
template_suffix => '.tt',
template_root => '__LIB__',
template_method_stub => '_template',
};
view_class
The class name of the particular template framework being used.
template_suffix
Override the suffix used for the template files (the default is provided by the view_class
)
template_root
Override the location where the template files are found. The string "__LIB__" will be replaced by the location of the installed modules, e.g.
template_root => '__LIB__/../root'
template_method_stub
Override the method name to use when specifying the template source with a local method.
See MooseX::Templated::Engine and MooseX::Templated::View for more information
SEE ALSO
REPOSITORY
https://github.com/sillitoe/moosex-templated
ACKNOWLEDGEMENTS
Chris Prather (perigrin)
AUTHOR
Ian Sillitoe <isillitoe@cpan.org>
LICENCE AND COPYRIGHT
Copyright (c) 2016, Ian Sillitoe <isillitoe@cpan.org>
. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.