Re: Setting a variable from within a template?
Roger Burton West <[email protected]> Thu, 23 Jul 2009 16:40:49 +0100
| Newsgroups | gmane.comp.lang.perl.modules.html-template |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Jul 23, 2009 at 03:56:36PM +0100, Stuart Moore wrote:
>Is there any way to set a variable within a template?
No. I tend to use my HTML::Template::Set extension, which is very basic
but gets the job done. I think this is the latest version. It allows you to:
<tmpl_set name=varname value=varvalue>
Roger
package HTML::Template::Set;
use HTML::Template;
use base qw(HTML::Template);
sub new {
my %set_params;
my $set_filter = sub {
my $text_ref=shift;
my $match='<(?:\!--\s*)?tmpl_set\s*name=(.*?)\s*value=(.*?)\s*>';
my @taglist=$$text_ref =~ m/$match/gi;
while (@taglist) {
my ($t,$v)=(shift @taglist,shift @taglist);
$set_params{$t}=$v;
}
$$text_ref =~ s/$match/<tmpl_if name=never><tmpl_var name=$1><\/tmpl_if>/gi;
};
my $proto = shift;
my $class = ref($proto) || $proto;
my $self=HTML::Template->new(filter => $set_filter,
@_);
bless ($self, $class);
$self->param(%set_params);
return $self;
}
1;
------------------------------------------------------------------------------