Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Re: HTML::Template question - tmpl_if

by djantzen (Priest)
on Dec 14, 2003 at 02:12 UTC ( #314597=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: HTML::Template question - tmpl_if
in thread HTML::Template question - tmpl_if

Well this is somewhat poor style, but you could say something like:

options => defined($hashref->{'options'}) || []

How about a nice map though that only pulls out the defined elements in your hash?

map { defined $hash->{$_} ? ($_ => $hash->{$_}) : () } keys %$hash;

The output then can be fed directly into param. This is limited too in that it assumes your hashkeys are identical to your fields (TMPL_VARS), but if you can live with that assumption it might work for you.

HTH


"The dead do not recognize context" -- Kai, Lexx


Comment on Re: Re: Re: HTML::Template question - tmpl_if
Select or Download Code
Re: Re: Re: Re: HTML::Template question - tmpl_if
by kiat (Vicar) on Dec 14, 2003 at 02:27 UTC
    Thanks, the first solution works for me.

    I didn't get the second solution to work. How do you get it work work given the code I gave about (the %hash and the generate subroutine).

    Finally, how would you have set up a template like this?

      Basically your generate subroutine would look like:

      sub generate { my $hashref = shift; my $template = HTML::Template->new(filename => "$tmpldir/generic.tt" +); $template->param(map { defined $hashref->{$_} ? ($_ => $hashref->{$_ +}) : () } keys %$hashref); ); print $template->output; }

      This works because HTML::Template::param expects a list an even number of elements in length suitable for hash type operations, and map of course produces lists, so our expression there utilizes the ternary operator (?:) to test for defined values and outputs key/value pairs.

      I am not really qualified to give advice about template design since I'm new to it myself. But just the fact you're using them I can comfortably say is a good sign :)


      "The dead do not recognize context" -- Kai, Lexx
        Many thanks, djantzen!

        I like the map code because it's much flexible than having to specify the values to be passed to Template's param().

        It's now working as expected :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://314597]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-09-16 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (10 votes), past polls