Thread: [htmltmpl] hierarchical template parameters patch
Brought to you by:
samtregar
From: Thilo P. <thi...@we...> - 2002-10-03 05:08:31
|
Hi, thanks for __counter__ and <tmpl_var default> in version 2.6. I'd like to suggest another feature: setting hashrefs as template parameters with the effect that HTML::Template retrieves all key/value-pairs from the hashref and sets corresponding parameters. The parameters follow a hierarchical naming scheme, so that you do not run into name-clashes and can easily keep track where the parameters are coming from. Use like this: $html->param('test'=> { 'a' => {'a' => 1, 'b' => 2}, 'b' => {'c'=>3, 'd'=>4} } ); to set <tmpl_var name='test/a/a'> <tmpl_var name='test/a/b'> <tmpl_var name='test/b/c'> <tmpl_var name='test/b/d'> Anyone else think this is useful? Cheers, Thilo Planz =================== Here is the patch: # if the value is a hashref, # construct parameters from its key/value-pairs # and set them using recursion if (ref $value and UNIVERSAL::isa($value, 'HASH')){ foreach (keys %$value){ $self->param(($param.'/'.$_)=>$value->{$_}); } return; } these lines go into HTML::Template (method param), just before # check that this param exists in the template $options->{die_on_bad_params} and !exists($param_map->{$param}) and croak("HTML::Template : Attempt to set nonexistent parameter '$param' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1)"); ====================== PS: I wouldn't try to stuff a hashref that contains recursive references into the method..... |
From: Mark M. <mm...@ol...> - 2002-10-03 17:06:41
|
Thilo, I think this is a great idea. Would it work for something like this: package Object; sub new { my ($class, $id) = @_; my $self = {}; bless $self, $class; $self->{name} = "name of object"; $self->{id} = $id; return $self; } package main; my $obj = new Object(1); $tmpl->param(object => $obj); On Wed, 2002-10-02 at 22:10, Thilo Planz wrote: > Hi, > > thanks for __counter__ and <tmpl_var default> in version 2.6. > > I'd like to suggest another feature: > setting hashrefs as template parameters with the effect that > HTML::Template retrieves all key/value-pairs from the hashref and sets > corresponding parameters. > The parameters follow a hierarchical naming scheme, so that you do not > run into name-clashes and can easily keep track where the parameters are > coming from. > > Use like this: > > $html->param('test'=> > { > 'a' => > {'a' => 1, 'b' => 2}, > 'b' => > {'c'=>3, 'd'=>4} > } > ); > > to set > > <tmpl_var name='test/a/a'> > <tmpl_var name='test/a/b'> > <tmpl_var name='test/b/c'> > <tmpl_var name='test/b/d'> > > Anyone else think this is useful? > > > Cheers, > > Thilo Planz > > =================== > > Here is the patch: > > # if the value is a hashref, > # construct parameters from its key/value-pairs > # and set them using recursion > if (ref $value and UNIVERSAL::isa($value, 'HASH')){ > foreach (keys %$value){ > $self->param(($param.'/'.$_)=>$value->{$_}); > } > return; > } > > these lines go into HTML::Template (method param), just before > > # check that this param exists in the template > $options->{die_on_bad_params} and !exists($param_map->{$param}) and > croak("HTML::Template : Attempt to set nonexistent parameter > '$param' - this parameter name doesn't match any declarations in the > template file : (die_on_bad_params => 1)"); > > ====================== > > PS: > > I wouldn't try to stuff a hashref that contains recursive references > into the method..... > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > -- Mark Mertel mm...@ol... Phone: (360) 344-3113 FAX: (360) 344-3553 Cell: (206) 409-2018 |