NAME
Template::Lace::Factory::InferInitArgsRole - fill init args by inspecting an object
SYNOPSIS
Create a template class:
package MyApp::Template::User;
use Moo;
with 'Template::Lace::ModelRole',
'Template::Lace::Factory::InferInitArgsRole',
has [qw/age name motto/] => (is=>'ro', required=>1);
sub template {q[
<html>
<head>
<title>User Info</title>
</head>
<body>
<dl id='user'>
<dt>Name</dt>
<dd id='name'>NAME</dd>
<dt>Age</dt>
<dd id='age'>AGE</dd>
<dt>Motto</dt>
<dd id='motto'>MOTTO</dd>
</dl>
</body>
</html>
]}
sub process_dom {
my ($self, $dom) = @_;
$dom->dl('#user', +{
age=>$self->age,
name=>$self->name,
motto=>$self->motto
});
}
1;
Create an object;
package MyApp::User;
has [qw/age name motto/] => (is=>'ro', required=>1);
1;
my $user = MyApp::User->new(age=>42,name=>'Joe', motto=>'Why?');
Use the object to create a render instance for your template:
my $factory = Template::Lace::Factory->new(
model_class=>'MyApp::Template::User');
my $renderer = $factory->create($user);
print $renderer->render;
Outputs:
<html>
<head>
<title>
User Info
</title>
</head>
<body id="body">
<dl id="user">
<dt>
Name
</dt>
<dd id="name">
Joe
</dd>
<dt>
Age
</dt>
<dd id="age">
42
</dd>
<dt>
Motto
</dt>
<dd id="motto">
Why?
</dd>
</dl>
</body>
</html>
DESCRIPTION
Allows you to fill your template arguments from an object, possibily saving you some tedious typing (at the possible expense of understanding).
In the (often likely) case that there is great interface compatibility between your business objects and template models, this role can save you the effort of writing a lot of tendious mapping code. Can save a lot when the interfaces contain a ton of fields. However you are reducing possible comprehension as well as introducing a possible evil interface binding into your code. The choice is yours :)
NOTE This works by using Moose and assumes your classes are written with Moo or Moose. This means you are adding a dependency on Moose into your project that you may not want. You will also inflate a meta object on your Moo class which may have some performance and memory usage implications.
SEE ALSO
AUTHOR
Please See Template::Lace for authorship and contributor information.
COPYRIGHT & LICENSE
Please see Template::Lace for copyright and license information.