Thread: [htmltmpl] Setting a variable from within a template?
Brought to you by:
samtregar
From: Stuart M. <st...@st...> - 2009-07-23 15:30:10
|
Is there any way to set a variable within a template? I'm looking to do something similar to <TMPL_SETVAR TITLE="My Page's Title"> <TMPL_INCLUDE 'header.tmpl'> To include a header template that has all the <HEAD> tags in (amongst other stuff). I can't see any way to do this - except possibly using some cunning stuff with HTML::Template::Expr, and even then it's not clear exactly how I'd do it. I'm prepared to write some kind of patch myself that would do it, but I thought it was worth checking if there was an existing method I'd overlooked. I've tried searching the mailing list, but no joy. (Apologies if I missed something) Stuart Moore |
From: Roger B. W. <ro...@fi...> - 2009-07-23 16:01:50
|
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; |
From: Justin S. <ju...@sk...> - 2009-08-10 06:48:44
|
I may be missing something terribly obvious, but the version of HTML::Template::Set that you pasted doesn't really work: #!/usr/bin/perl use strict; my $tmpl = '<!-- tmpl_set name="foo" value="bar" --><h1><!-- tmpl_var foo--></h1>'; my $template = HTML::Template::Set->new( scalarref => \$tmpl, ); print $template->output; Gives you an error: HTML::Template->new() : fatal error occured during filter call: "foo" at test.pl line 6 at test.pl line 6 The version of HTML::Template::Set that's on CPAN right now is from 2004 and is a lot, and I mean a LOT different (and complex). I liked your version better, but it doesn't seem to be doing the right thing, sadly. Justin On Jul 23, 2009, at 9:40 AM, Roger Burton West wrote: > 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; > > > ------------------------------------------------------------------------------ > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |
From: Justin S. <ju...@sk...> - 2009-08-10 07:00:22
|
Sorry, the code does work, there was a simple error in my template (drrr) #!/usr/bin/perl use strict; use HTML::Template::Set; my $tmpl = '<!-- tmpl_set name="foo" value="bar" --><h1><!-- tmpl_var foo --></h1>'; my $template = HTML::Template::Set->new( scalarref => \$tmpl,); print $template->output; There's still some problems with this approach. One being that any other filters you attempt to use, don't work. I think. I'm still trying to 'rastle it into my code. It makes almost more sense to just use this simple idea *as* a filter, instead of using it as a separate module. Would make it work easier with H::T::Expr as well. The problem is the filter saves the name/values you need to add to param() sometime, later. I can't quite figure a nice way to do this, without global vars. I have this very weird feeling I'm having a conversation that's been repeated a few... hundred times, already. On wards! Justin On Aug 10, 2009, at 12:05 AM, Justin Skazat wrote: > I may be missing something terribly obvious, but the version of > HTML::Template::Set that you pasted doesn't really work: > > > > #!/usr/bin/perl > use strict; > my $tmpl = '<!-- tmpl_set name="foo" value="bar" --><h1><!-- tmpl_var > foo--></h1>'; > my $template = HTML::Template::Set->new( > scalarref => \$tmpl, > ); > print $template->output; > > Gives you an error: > > HTML::Template->new() : fatal error occured during filter call: "foo" > at test.pl line 6 > at test.pl line 6 > > > The version of HTML::Template::Set that's on CPAN right now is from > 2004 and is a lot, and I mean a LOT different (and complex). I liked > your version better, but it doesn't seem to be doing the right thing, > sadly. > > Justin > > > > > On Jul 23, 2009, at 9:40 AM, Roger Burton West wrote: > >> 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; >> >> >> ------------------------------------------------------------------------------ >> _______________________________________________ >> Html-template-users mailing list >> Htm...@li... >> https://lists.sourceforge.net/lists/listinfo/html-template-users >> > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |