Reusing HTML Between Views

When you need to return HTML generated in Catalyst::View::TT to use in another view (maybe Catalyst::View::JSON), you should isolate the HTML into its own template file so you can render the file directly from the controller, like so.....

my $html = $c->view('TT')->render($c,'list_main.tt2', { result => $result, no_wrapper => 1 });

The hashref above replaces the stash, no_wrapper is there so you can manually return "just content" if WRAPPER is set in Catalyst::View::TT's config. see disabling a tt wrapper for ajax requests for more information.

Actions using Catalyst::View::TT will use the top level template file for list_main.tt2 in this case list.tt2, which would look something similar to this...

    [% FOREACH result IN results.all -%]
        [% INCLUDE 'list_main.tt2' -%]
    [% END -%]
[% INCLUDE pager.tt2 -%]

As you can see list.tt2 includes list_main.tt2 for each result. A single result can now be rendered individually and the returned HTML can then be used with a different view.

This could easily be adapted to return the HTML for a ResultSet, the main thing to take away from this is $c->('View::TT')->render() can be used directly to return the output of Template Toolkit without the output being inserted into Catalyst's response body.

My tags:
Popular tags:
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker