MooseX::Templated - pre-CPAN
[email protected] ("Ian Sillitoe")
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
This is a follow up on a previous thread ("Moose::Role::TTSelf"). With
peregrin's help (and considerable patience - many thanks) - I think
MooseX::Templated is pretty much at the point of pushing to CPAN. I'm
forwarding this to this list in case anyone has any more
comments/suggestions before I tip it over the edge (synopsis appended below)
http://code2.0beta.co.uk/moose/svn/MooseX-Templated/
Cheers,
Ian
=head1 NAME
MooseX::Templated::Role - Framework to render Moose objects with templates
=head1 SYNOPSIS
Farm/Cow.pm
package Farm::Cow;
use Moose;
with 'MooseX::Templated::Role';
has 'spots' => ( is => 'rw' );
has 'hobbies' => ( is => 'rw', default => sub { ['mooing', 'chewing'] } );
sub moo { "Moooooooo!" }
Farm/Cow.t
This cow has [% self.spots %] spots. It mostly spends its time
[% self.hobbies.join(" and ") %]. However, when it is very happy
it exclaims, "[% self.moo %]!".
Elsewhere on the Farm...
my $cow = Farm::Cow->new( spots => '8' );
$cow->render();
# This cow has 8 spots. It mostly spends its time
# mooing and chewing. However, when it is very happy
# it exclaims, "Moooooooo!".
Alternatively, back in the cowshed
package Farm::Cow;
<snip>
sub _templated_xml {
return <<"_XML_TT";
<cow sound="[% self.moo | html %]" spots="[% self.spots %]">
[%- FOREACH hobby = self.hobbies %]
<hobby name="[% hobby | html %]"/>
[%- END %]
</cow>
_XML_TT
}
Back out on the farm
$cow->render( 'xml' );
# <cow sound="Moooooooo" spots="8">
# <hobby name="mooing"/>
# <hobby name="chewing"/>
# </cow>