Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

HTML::Template: group <tmpl_var>s

by Spidy (Chaplain)
on Nov 09, 2006 at 17:34 UTC ( #583168=perlquestion: print w/replies, xml ) Need Help??
Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings fellow monks,

I've been doing some work with HTML::Template lately, and one piece of it that I found to be immensely useful was the <tmpl_loop> tag. It made it easy to group together all of the variables I would need for specific areas of my output that were to be repeated. However, I have a question:

Is there a way to do this other than using a <tmpl_loop>? I have a piece of template that won't be repeated, and it would be easier to fill it in the same way you fill in <tmpl_loop> tags than to have multiple param() calls.


Replies are listed 'Best First'.
Re: HTML::Template: group <tmpl_var>s
by rhesa (Vicar) on Nov 09, 2006 at 18:06 UTC
    HTML::Template offers a number of ways of setting params. You can call param() for every pair of names and values, or pass in several of those pairs in one go:
    # set one variable: $t->param( some => 'value' ); # set a number of them $t->param( one => 'value', two => 'value', three => 'value', ); # pass in an existing hash $t->param( %lots_of_values ); # pass in a reference to a hash $t->param( $ref_to_hash );
    As you can see, there are a number of ways to group your input.
Re: HTML::Template: group <tmpl_var>s
by stonecolddevin (Vicar) on Nov 09, 2006 at 18:20 UTC

    I think I know what you mean. I use <TMPL_LOOP> not only when I'm doing actual loops, but to speed up the coding when retrieving, say, a single user's information from a database. I don't think there's any better way, because this isn't really a bad way to do it in the first place.

    I always use something like:

    $tmpl->param( filename => 'one_users_information.tmpl', user =>\@userinfo );


Re: HTML::Template: group <tmpl_var>s
by tinita (Parson) on Nov 09, 2006 at 17:51 UTC
    could you give a small example of what you want to do?
Re: HTML::Template: group <tmpl_var>s
by skx (Parson) on Nov 09, 2006 at 19:03 UTC

    You could just put all the parameters into place with one call, like this:

    $template->param( foo => "bar", username => "steve", is_admin => 0 );

    Sure that doesn't use loops, or arrays as the tmpl_loop construct would, but you've avoided multiple param() calls.

Re: HTML::Template: group <tmpl_var>s
by friedo (Prior) on Nov 09, 2006 at 18:06 UTC
    You might want to take a look at HTML::Template::Plugin::Dot which lets you access complex structures with Template-Toolkit-like syntax. Then you can just pass an array or hash to your template and access it without looping.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://583168]
Approved by ikegami
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-06-19 03:02 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.