in reply to HTML::Template question - tmpl_if

ok, several comments --

One, there is an H::T list. If you are going to use H::T a lot (and it would be great if you do), I would encourage you to join the list because the focus of all the members of the list is nothing but H::T.

repeatedly, in your code you have a trailing comma in while defining your hash. I am surprised you are not getting an error for that.

%hash ( des => 'Form 2', # field label label => 'Label 2', # name of field field => 'field2', # size of text input field value => '10', submit => 'submit', options => \@options, if_more => 0, );
that last comma, the one after if_more => 0, should not be there.

Look at what the error message is saying -- it is telling you that you are trying to set parameter 'options' with a scalar, but that parameter is not a TMPL_VAR. Well, look at your template... 'options' is indeed not a TMPL_VAR but a TMPL_LOOP, and a TMPL_LOOP expects a ref to an array of hashrefs. If you give it a scalar (which, you inadvertently do by not setting a value for options in your second example), the code will croak.

hth

Replies are listed 'Best First'.
Re: Re: HTML::Template question - tmpl_if
by djantzen (Priest) on Dec 14, 2003 at 18:15 UTC

    Actually perl doesn't mid a bit if you have too many commas in a list, and in fact I've heard monks advocate a trailing comma to make it easier later to expand the list. I avoid doing it myself but mostly for aesthetic reasons.

    use strict; use warnings; my @list = ('foo', 'bar',,,,'baz',,,,); print "Item $_\n" for @list; print "Length " . scalar @list."\n";
    Note that perl even gets the length of the array correct.


    "The dead do not recognize context" -- Kai, Lexx
      I stand corrected. And, I agree with you -- since I have always left the trailing comma out, I will continue to leave it out. Aesthetically it is more pleasing, and logically it achieves a closure. Good for the heart and the mind.
Re: Re: HTML::Template question - tmpl_if
by kiat (Vicar) on Dec 15, 2003 at 00:32 UTC
    Thanks, punkish!

    I did a dump of @options.

    # This dumped output of options is #$VAR1 = { 'school' => 'Admiralty Primary', 'school_id' => '1' }; #$VAR2 = { 'school' => 'Ahmad Ibrahim Primary', 'school_id' => '2' }; #$VAR3 = { 'school' => 'Anderson Primary', 'school_id' => '3' }; # the relevant template is # note that the school_id in <select name="school_id"> is # hard-coded whereas the one in <tmpl_var name=school_id>"> # is a number supplied via @options <select name="school_id"><option selected value="1"><option selected v +alue="0">Select<tmpl_loop name=options> <option value="<tmpl_var name=school_id>"><tmpl_var escape=html name=s +chool></option></tmpl_loop> </select></td></tr>
    The above works with the template but I'm not sure if I'm missing something. Note that %hash is passed to &generate (thanks to djantzen for the map) which generates a list which is then supplied to the param of H::T.
      I am not sure what your question is this time around. You write...
      The above works with the template but I'm not sure if I'm missing something.
      If it works, then you are not missing anything. Your template code is, though, a bit confusing. You have typed (perhaps, a typo) --
      <select name="school_id"><option selected value="1"><option selected v +alue="0">Select<tmpl_loop name=options> <option value="<tmpl_var name=school_id>"><tmpl_var escape=html name=s +chool></option></tmpl_loop> </select>
      Perhaps you meant --
      <select name="school_id" size="1"> <option selected value="0">Select school</option> <tmpl_loop options> <option value="<tmpl_var school_id>"><tmpl_var escape=html school> +</option> </tmpl_loop> </select>
      Wrt your original question, no matter how you do it, just remember that in H::T each variable is a scalar, while each loop is an array of hash(es). You'll be ok. If you want to not set anything, you can do one of two things -- turn die_on_bad_params off by setting it to 0, or better yet, make sure you set the value to an empty reference. Actually, you can do one more thing -- use tmpl_if to check for existence of a var before trying to display it. Good luck.