Catalyst::View::Template::Lace::Role::ArgsFromStash - fill init args from the stash


Create a View that does this role:

    package  MyApp::View::User;

    use Moo;
    extends 'Catalyst::View::Template::Lace';
    with 'Catalyst::View::Template::Lace::Role::ArgsFromStash',

    has [qw/age name motto/] => (is=>'ro', required=>1);

    sub template {q[
          <title>User Info</title>
          <dl id='user'>
            <dd id='name'>NAME</dd>
            <dd id='age'>AGE</dd>
            <dd id='motto'>MOTTO</dd>

    sub process_dom {
      my ($self, $dom) = @_;
      $dom->dl('#user', $self);

      ## The following two options all do the same transformation
      ## just maybe a bit more typing (you get a speedup since ->fill
      ## does not need to inspect '$self' for its fields.

      # $dom->dl('#user', +{
      #   age=>$self->age,
      #   name=>$self->name,
      #   motto=>$self->motto
      # });

      # $dom->at('#user')
      #   ->fill({
      #     age=>$self->age,
      #     name=>$self->name,
      #     motto=>$self->motto,
      #   });


Call the View from a controller that sets stash values;

    package MyApp::Controller::User;

    use Moose;
    use MooseX::MethodAttributes;
    extends 'Catalyst::Controller';

    sub display :Path('') {
      my ($self, $c) = @_;
        name => 'John',
        age => 42,
        motto => 'Why Not?');


Produces result like:

        <title>User Info</title>
        <dl id="user">
          <dd id="name">
          <dd id="age">
          <dd id="motto">
            Why Not?


If you wish to create a view using arguments passed from the Catalyst stash, you can do so with this role. You may find this role helpful since its been common to use the stash to pass values to the view for a long time. Although I prefer to avoid use of the stash, in this case at least the stash values are required to match the view type so there's less downside here than in other common views.

You might find this useful when you are trying to integrate Catalyst::View::Template::Lace into an existing project that makes heavy use of the stash, or when you build up data for the view across actions in an action chain (although also see Catalyst::View::Template::Lace::Role::PerContext).

Also see "process" in Catalyst::View::Template::Lace for more support for 'classic' style template use.




Please See Catalyst::View::Template::Lace for authorship and contributor information.


Please see Catalyst::View::Template::Lace for copyright and license information.