porton porton - 4 months ago 6x
Perl Question

Explicit calling a view in Catalyst

Reading http://search.cpan.org/~ether/Catalyst-Manual-5.9009/lib/Catalyst/Manual/Tutorial/02_CatalystBasics.pod I see

$c->stash(template => 'hello.tt');

There is a template, but where is the view? Why is the view not specified explicitly?

Also: How to call a view explicitly (not by the template name)?


This can be a little tricky because it is a combination of extremely flexible and plain DWIW. The .tt in the template name indicates that Template::Toolkit is the presumptive default. It's not necessary to use this View at all but it is used in most of the documentation.

You can render whatever is in your stash/$ctx by forwarding to a view: $c->forward($c->view("JSON")); for example, or you can decide for the rest of the request cycle like this $c->stash( current_view => "CSV" ). You have to have the view(s) installed and configured in your application, of course. You can also use views directly-

my $body = $c->view("Xslate")->render($c, "folder/email_template.tt", $params );

A tricky part becomes what your end method(s) will do.

The generally recommended default is Catalyst::Action::RenderView. It is smart (and flexible if you want to change its behavior). If you have set a redirect, for example, it won't render a page, it will just do the redirect. If you have already generated $c->response->body content it will also "just stop" and trust that what you've already done is what you wanted.

There is enough complexity in the flexibility that I really recommend just putting together a toy app run in the restarting dev server and trying out all the things you want to understand better. None of it is that hard in itself but it all adds up to confusion unless you break it down into digestible chunks.