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

Basically your generate subroutine would look like:

sub generate { my $hashref = shift; my $template = HTML::Template->new(filename => "$tmpldir/" +); $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

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: HTML::Template question - tmpl_if
by kiat (Vicar) on Dec 14, 2003 at 04:23 UTC
    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 :)