Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

How do I cope with nested TMPL_VAR in HTML::Template::Pro?

by PerlOnTheWay (Scribe)
on Aug 03, 2011 at 14:38 UTC ( #918281=perlquestion: print w/replies, xml ) Need Help??
PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

What I want to do is like this:

<TMPL_LOOP NAME="loopname"> <TMPL_VAR NAME="<TMPL VAR NAME="anothervariable">"> </TMPL_LOOP>

But sadly it doesn't work out of the box of HTML::Template::Pro.

The reason I need to do it here is that the hash key is the value of another variable($hash{$key}).

Is there a way to cope with this elegantly?

Replies are listed 'Best First'.
Re: How do I cope with nested TMPL_VAR in HTML::Template::Pro?
by moritz (Cardinal) on Aug 03, 2011 at 14:46 UTC
    Is there a way to cope with this elegantly?

    Preprocess the data you send to the template system to form that the template system can understand.


      There's no way to change a variable to a constant,right?

        If you template snippet would work, the data input for it would look like
        $tmpl->param(loopname => [ { anothervariable => 'foo', foo => 'actual value', }, ... ])

        You need to change that to

        $tmpl->param(loopname => [ { fixed_key => 'actual value', }, ... ])

        For example by creating a fixed coyp of each hashref like this:

        my $new = { fixed_key => $old->{$old->{anothervariable}} };
Re: How do I cope with nested TMPL_VAR in HTML::Template::Pro?
by sundialsvc4 (Abbot) on Aug 03, 2011 at 15:22 UTC

    One way that I found to do something like this (in the context of Template::Toolkit, as it so happens) is through the idea of closures.

    In other words, within the Perl sub that prepares my screen for display, I set up various statements like my $closure = sub { ... }, thus creating code-references.   I pass those variables to the template.   This templating system knows that, when a particular variable’s value is a code-ref, it’s supposed to call that routine and use its result.   (I have no reason to believe that the templating system you’re using won’t be similar.)   Hence, the calls occur, and since the closures are defined within an outer sub, they have access to all of the local (my) variables within it.   They are, then, “part of” that subroutine, but they are called by the template as it is doing its work.

    There is always a delicate balance between what logic you want to put into a template-file and what logic you want to put into the application.   This strategy has allowed me to, in a way, have some of the best of both worlds.   (And it is heavily documented in both sets of files.)   The template avoids cross-dependency regarding how the data is being stored, because it has access to a simple function that gives it the right answer.   If I have to change that answer or how it is derived, I only must change it in one place.

      Can you use a small example to illustrate?
Re: How do I cope with nested TMPL_VAR in HTML::Template::Pro?
by Anonymous Monk on Aug 03, 2011 at 14:45 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-06-22 19:07 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (528 votes). Check out past polls.