develooper Front page | perl.beginners | Postings from March 2019

Re: proper use of TEMPLATE

Thread Previous | Thread Next
David Precious
March 19, 2019 12:44
Re: proper use of TEMPLATE
Message ID:
On Thu, 14 Mar 2019 12:14:59 -0500
Rick T <> wrote:
> <option value="1" [% bool_full_selected %]> Full Course (1
> credit)</option>
> My code can easily replace the correct var with ‘selected’ and the
> other vars on the list with a blank. But what should that blank be?
> Should I make it an empty string, a space, undef, or something else?

An empty string would work just fine.

On the other hand, your template var name there suggests it's a
boolean, indicating whether or not the full course is selected, as
opposed to containing the word "selected" or an empty string.

I'd be inclined to actually make it so, and have the "what do we output
if it *should* be selected" be in the template, e.g.:

<option value="1" [% IF bool_full_selected %]selected[% END %]>....

Also, as we've not seen your full code, it's not clear how you're
generating the list of options, but you can iterate over a list or
names or hashrefs of information about each entry.

You could, for instance, have a list of hashrefs of information about
each course - the value to use in the value="" attribute, the course
name to display, etc... something like:

  my %courses = (
    { id => 1, name => "Full Course", selected => 1 },
    { id => 2, name => "Half Course", },

(Of course, it's quite likely that in a production system, that
information will have been assembled from a database query or a config
file rather than hard-coded, but you get the idea.)

You could pass that on to your Template->process call, then in the
template itself, output the options with something like:

  [% FOREACH course IN courses %]
    <option value="[% %]"
      [% IF course.selected %]selected[% END %]>[% %]
  [% END %]

That way, your template doesn't need to know/care how many course
options there are or what they are, it only needs to know/care how to
*present* them.


Dave P

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About