Catalyst-View-Text-MicroTemplate-PerRequest

 view release on metacpan or  search on metacpan

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


    return sub {
      my ($view, $ctx) = @_;
      return $ctx->response->status > 299 ?
        "${\$ctx->action}_${\$ctx->res->status}" :
          "${\$ctx->action}";
    };

That way if an action returns for various status, you can distinguish between OK and
error or exception responses.

BTW, this complication points out that it is possible that the idea of allowing a view
to set a default template via its internal logic is suspect.  The issue here is that
if you are seeking to write actions that can be used across different views (like an HTML
view that needs a template and a JSON view that doesn't) you really prefer to not have
code in your action that dictates a template choice.

=head1 ATTRIBUTES

This View defines the following attributes that can be set during configuration

=head2 merge_stash

Boolean, defaults to false.  If enabled merges anything in $c->stash to
the template arguments.  Useful if you love the stash or have a situation
where you want to start using this view in an existing application that makes
extensive use of stash.

=head2 path_base

Location of your templates.  Defaults to whatever $application->config->{root}
is.

=head2 extension

File extention that your templates use.  Defaults to ".mt".  Please note the '.'
is required.

=head2 content_type

The default content type of your view.  Used when providing a response and the
content type is currently undefined.  Defaults to 'text/plain'.

=head2 template_args

An option hashref of arguments that are always provided to your template.  See
L<https://metacpan.org/pod/Text::MicroTemplate::Extended#template_args1> for more.

Your template always gets an arg '$c' which is the current context. You may also
provide per context template args by providing a method 'extra_template_args'.  The
advantage of that approach is that method will get the context and other objects
as arguments which makes it easier to provide context sensitive values.

=head2 default_template_factory

Default value for L</template_factory>.  Useful if you want a global override for
this.

=head2 macros

See L<https://metacpan.org/pod/Text::MicroTemplate::Extended#Macro>

=head2 mt

This is the L<Text::MicroTemplate::Extended> object.  You probably want to leave this
alone but you can set it as you wish as long as you provide a compatible interface.
You may find this useful for things like mocking objects during testing.

=head2 default_view_model

The L<Catalyst> model that is the default model for holding information to
pass to the view.  The default is L<Catalyst::Model::Text::MicroTemplate::ViewData>
which exposes a stash like interface based on L<Data::Perl::Role::Collection::Hash>
which is good as a basic interface but you may prefer to try a define a more
strict view model interface.

If you create your own view model, it should define a method, 'TO_HASH' to
provide a hash suitable to pass as arguments to the template.   This allows you
to separate the view model from the data passed.  If you don't define such a
method your view model object will be passed to the template as the first
argument in '@_';

=head2 mt_class

The class used to create the L</mt> object.  Defaults to L<Text::MicroTemplate::Extended>
You can set this to whatever you wish but it should be a compatible interface.

=head2 mt_init_args

Arguments used to initialize the L</mt_class> in L</mt>.  Should be a hashref.
See L<Text::MicroTemplate::Extended> for available options

=head2 handle_process_error

A reference to a subroutine that is called when there is a failure to render
the data given.  This can be used globally as an attribute
on the defined configuration for the view, and you can set it or overide the
global settings on a context basis.

Setting this optional attribute will capture and handle error conditions.  We
will NOT bubble the error up to the global L<Catalyst> error handling (we don't
set $c->error for example).  If you want that you need to set it yourself in
a custom handler, or don't define one.

The subroutine receives two arguments: the view object and the exception. You
must setup a new, valid response.  For example:

    package MyApp::View::HTML;

    use Moo;
    extends 'Catalyst::View::Text::MicroTemplate::PerRequest';

    package MyApp;

    use Catalyst;

    MyApp->config(
      default_view =>'HTML',
      'View::HTML' => {
        handle_process_error => sub {
          my ($view, $err) = @_;



( run in 0.515 second using v1.01-cache-2.11-cpan-6aa56a78535 )