File Coverage

blib/lib/Catalyst/View/Template/Lace/Role/ArgsFromStash.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Catalyst::View::Template::Lace::Role::ArgsFromStash;
2              
3 1     1   421 use Moo::Role;
  1         3  
  1         7  
4              
5             around BUILDARGS => sub {
6             my ( $orig, $class, @args ) = @_;
7             my $args = $class->$orig(@args);
8             my %stash_args = $args->{ctx} ? %{$args->{ctx}->stash} : ();
9             $args = +{
10             %$args,
11             %stash_args};
12             return $args;
13             };
14              
15             1;
16              
17             =head1 NAME
18              
19             Catalyst::View::Template::Lace::Role::ArgsFromStash - fill init args from the stash
20              
21             =head1 SYNOPSIS
22              
23             Create a View that does this role:
24              
25             package MyApp::View::User;
26              
27             use Moo;
28             extends 'Catalyst::View::Template::Lace';
29             with 'Catalyst::View::Template::Lace::Role::ArgsFromStash',
30              
31             has [qw/age name motto/] => (is=>'ro', required=>1);
32              
33             sub template {q[
34             <html>
35             <head>
36             <title>User Info</title>
37             </head>
38             <body>
39             <dl id='user'>
40             <dt>Name</dt>
41             <dd id='name'>NAME</dd>
42             <dt>Age</dt>
43             <dd id='age'>AGE</dd>
44             <dt>Motto</dt>
45             <dd id='motto'>MOTTO</dd>
46             </dl>
47             </body>
48             </html>
49             ]}
50              
51             sub process_dom {
52             my ($self, $dom) = @_;
53             $dom->dl('#user', $self);
54              
55             ## The following two options all do the same transformation
56             ## just maybe a bit more typing (you get a speedup since ->fill
57             ## does not need to inspect '$self' for its fields.
58              
59             # $dom->dl('#user', +{
60             # age=>$self->age,
61             # name=>$self->name,
62             # motto=>$self->motto
63             # });
64              
65             # $dom->at('#user')
66             # ->fill({
67             # age=>$self->age,
68             # name=>$self->name,
69             # motto=>$self->motto,
70             # });
71             }
72              
73             1;
74              
75             Call the View from a controller that sets stash values;
76              
77             package MyApp::Controller::User;
78              
79             use Moose;
80             use MooseX::MethodAttributes;
81             extends 'Catalyst::Controller';
82              
83             sub display :Path('') {
84             my ($self, $c) = @_;
85             $c->stash(
86             name => 'John',
87             age => 42,
88             motto => 'Why Not?');
89             $c->view('User')
90             ->respond(200);
91             }
92              
93             __PACKAGE__->meta->make_immutable;
94              
95             Produces result like:
96              
97             <html>
98             <head>
99             <title>User Info</title>
100             </head>
101             <body>
102             <dl id="user">
103             <dt>Name</dt>
104             <dd id="name">
105             John
106             </dd>
107             <dt>Age</dt>
108             <dd id="age">
109             42
110             </dd>
111             <dt>Motto</dt>
112             <dd id="motto">
113             Why Not?
114             </dd>
115             </dl>
116             </body>
117             </html>
118              
119             =head1 DESCRIPTION
120              
121             If you wish to create a view using arguments passed from the L<Catalyst> stash, you can
122             do so with this role. You may find this role helpful since its been common to use the
123             stash to pass values to the view for a long time. Although I prefer to avoid use of the
124             stash, in this case at least the stash values are required to match the view type so
125             there's less downside here than in other common views.
126              
127             You might find this useful when you are trying to integrate L<Catalyst::View::Template::Lace>
128             into an existing project that makes heavy use of the stash, or when you build up data for
129             the view across actions in an action chain (although also see
130             L<Catalyst::View::Template::Lace::Role::PerContext>).
131              
132             Also see L<Catalyst::View::Template::Lace/process> for more support for 'classic' style
133             template use.
134              
135             =head1 SEE ALSO
136            
137             L<Catalyst::View::Template::Lace>.
138              
139             =head1 AUTHOR
140              
141             Please See L<Catalyst::View::Template::Lace> for authorship and contributor information.
142            
143             =head1 COPYRIGHT & LICENSE
144            
145             Please see L<Catalyst::View::Template::Lace> for copyright and license information.
146              
147             =cut