[Catalyst] Getting the result of the template processing

Marc Logghe Marc.Logghe at DEVGEN.com
Wed Jan 3 10:40:04 GMT 2007


> -----Original Message-----
> From: Xavier Robin [mailto:robin0 at etu.unige.ch] 
> Sent: Wednesday, January 03, 2007 10:46 AM
> To: catalyst at lists.rawmode.org
> Subject: [Catalyst] Getting the result of the template processing
> 
> Hello all,
> 
> How to get the result of the processing of a template ?
> 
> I would like to do someting like that:
> 
> $c->stash->{template} = 'display.tt2';
> my $result = $c->process_template;
> # put $result in database
> 
> The processing of display.tt2 is quite long (a couple of 
> seconds to go through nearly the whole database), so I would 
> like to do it only once.
> 
> I'm using a TTSite view. I don't want to get the complete 
> page, only the HTML code generated by the template. What 
> command should I use in place of "$c->process_template"? I 
> couldn't find any plugin to do that, but not sure I looked 
> deeply enough to find it, though...

Hi Xavier,
I did something similar like you are trying to do. Only difference is
that the result was written to a file instead of being stored in a
database.
First you have to make sure that the output of your template is not
wrapped, because you are not interested in the complete HTML.
In order to achieve that, this thread might be of help:
http://thread.gmane.org/gmane.comp.web.catalyst.general/9823/

Second, to get your hands on the result after rendering you could do it
like this:

# only want the output from the template,
# no wrapping wanted
$c->stash->{nowrap} = 1;

$c->stash->{template} = 'display.tt2';
# force rendering
# by doing that, the body is set
$c->forward($c->view);
# get the rendered result from the body
my $result = $c->response->body;
# store $result in the database
# ...

In my case, I just needed the unwrapped content as the response to an
AJAX call. I suppose you need it wrapped but you don't want to make the
calculation again, right ?
In that case, after running the code above, what you *could* do is:
1) reset the body
2) copy $result into the stash (e.g. $c->stash->{content} = $result;)
3) set the template to 'wrap_content.tt2' and forward to that one
4) reset nowrap ($c->stash->{nowrap} = 0;)
The custom template wrap_content.tt2 simply looks like
[% c.content %]
In that way your content gets wrapped and passed to the browser.

Hope all that makes sense ;-)
Regards,
Marc




More information about the Catalyst mailing list