Subject: | Useful capture subroutine |
Here's a little tool I sometimes use with Template::Declare to allow me
to capture a little bitty template for use a text elsewhere in some
cases. See
http://github.com/zostay/qublog/tree/revamp-journal-look/lib/Qublog/View.pm
for some examples on how I use it. The specific use case here is that I
need to assemble a list of items that are sorted by date, but reside in
several tables, so I'm not sure of the sort until I do it and the text
placed inside the templates once ready to render might need to be
templates. I use capture {} here in a similar way to the use of defer {}
in model definitions.
Usage is:
my $html = capture { p { 'some text } };
Definition is:
sub capture(&) {
my $code = shift;
my $content;
Template::Declare->new_buffer_frame;
{
$code->();
$content = Template::Declare->buffer->date || '';
}
Template::Declare->end_buffer_frame;
return $content;
}