Catalyst-View-Text-MicroTemplate-PerRequest

 view release on metacpan or  search on metacpan

lib/Catalyst/View/Text/MicroTemplate/PerRequest.pm  view on Meta::CPAN

        || "${\$ctx->action}";
    };
  });

sub COMPONENT {
  my ($class, $app, $args) = @_;
  $args = $class->merge_config_hashes($class->config, $args);
  $args->{app} = $app;
  $class->_inject_default_view_model_into($app);
  return $class->new($app, $args);
}

sub _inject_default_view_model_into {
  my ($class, $app) = @_;
  CatalystX::InjectComponent->inject(
    into => $app,
    component => 'Catalyst::Model::Text::MicroTemplate::ViewData',
    as => 'Model::Text::MicroTemplate::ViewData' );
}

sub _create_has_process_error_param {
  my $self = shift;
  return $self->has_handle_process_error ?
    (handle_process_error=>$self->handle_process_error) : ();
}

sub build_per_context_instance {
  my ($self, $c, @args) = @_;
  return bless +{
    ctx=>$c,
    parent=>$self,
    mt=>$self->mt,
    ($self->_create_has_process_error_param),
  }, 'Catalyst::View::Text::MicroTemplate::_PerRequest';
}

1;

=head1 NAME

Catalyst::View::Text::MicroTemplate::PerRequest - JSON View that owns its data 

=head1 SYNOPSIS

    MyApp->inject_components(
      'View::HTML' => { from_component => 'Catalyst::View::Text::MicroTemplate::PerRequest' }
    );

    # In a controller...

    sub root :Chained(/) CaptureArgs(0) {
      my ($self, $c) = @_;
      $c->view('HTML')->data->set(z=>1);
    }

    sub midpoint :Chained(root) CaptureArgs(0) {
      my ($self, $c) = @_;
      $c->view('HTML')->data->set(y=>1);
    }

    sub endpoint :Chained(midpoint) Args(0) {
      my ($self, $c) = @_;
      $c->view('JSON')->created({
        a => 1,
        b => 2,
        c => 3,
      });
    }

    # template $HOME/root/endpoint.mt
    
    This is my template
    Here's some placeholders
    a => <?= $a ?>
    b => <?= $b ?>
    c => <?= $c ?>
    y => <?= $y ?>
    z => <?= $z ?>


=head1 DESCRIPTION

This is a L<Catalyst::View> that uses L<Text::MicroTemplate::Extended> which
is a pure Perl templating engine that uses Perl code instead of a dedicated
template domain specific language.  You may find this a bad idea for large
projects where you have a dedicated front end development team (or just a bad
idea in general).  However I find for smaller projects where the back end programmer
does double duty as a UI developer its useful to avoid having to remember yet
another DSL just to do output formating.  Its reasonable fast and if you control
yourself you won't make too much of a mess.  You should review L<Text::MicroTemplate::Extended>
and L<Text::MicroTemplate> for more on how the template engine works.

It differs from other L<Catalyst> views on CPAN (including L<Catayst::View::Text::MicroTemplate>)
In that it is a 'per request view' that lets you define a view owned data model
for passing information to the view.  You may find this a better solution than
using the stash although you may also use the stash if you like (there's a template
setting for this.)

It also generates some local response helpers.  You may or may not find this
approach leads to cleaner code.  Lastly we allow you to more easily override
how we generate a default template name.  In general I consider this view to be
a somewhat experimental approach to solve some common problems that have really irked
me about the commonly used approach to views in L<Catalyst>  However I do use this in
product so I commit to making this stable and safe (just this is not the official proscribed
way so you might find some learning curve).

A similar style view that produces JSON is L<Catalyst::View::JSON::PerRequest>.
In fact this was written so that I could have a view that did HTML with an identical
interface as that JSON view, to make it easier in situations where I want to choose
a view based on content negotiation (like when doing an API) but would like to keep
the rest of the code the same.

I consider this approach to be an interesting experiment in alternative ways to
use Catalyst views.

=head1 METHODS

This view defines the following methods

=head2 template

Used to override the default template, which is derived from the terminating
action.  You can set this to a particular template name, or an $action.

=head2 data (?$model)

Used to set the view data model, and/or to called methods on it (for example
to set attributes that will later be used in the  response.).

The default is an injected model based on L<Catalyst::Model::Text::MicroTemplate::ViewData>



( run in 0.796 second using v1.01-cache-2.11-cpan-39bf76dae61 )