MooseX::Templated - template-based rendering of Moose objects
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
The MooseX::Templated role provides the consuming class with a method render() which allows template-based rendering of the object.
MooseX::Templated
render()
The following methods are provided to the consuming class
Returns MooseX::Template::Engine which is the templating engine responsible for rendering the template.
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).
The template engine will search for the template source in a few different locations: files, methods, inline.
Farm::Cow->new()->render()
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.
__LIB__
The file path can be affected by configuration options: template_root, template_suffix
template_root
template_suffix
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.
"_template"
sub Farm::Cow::_template { ... }
The expected method name is affected by configuration option: template_method_stub.
template_method_stub
Provide the template source directly to the render function (as a reference to the template string).
Farm::Cow->render( \"Cow goes [% self.moo %]!" );
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', };
The class name of the particular template framework being used.
Override the suffix used for the template files (the default is provided by the view_class)
view_class
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'
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
Moose, Template
https://github.com/sillitoe/moosex-templated
Chris Prather (perigrin)
Ian Sillitoe <isillitoe@cpan.org>
<isillitoe@cpan.org>
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.
To install MooseX::Templated, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Templated
CPAN shell
perl -MCPAN -e shell install MooseX::Templated
For more information on module installation, please visit the detailed CPAN module installation guide.