experchange > perl

Rick T (03-14-19, 07:14 PM)
I’m sure this is a “newbie question,” but I am only a shade above that status and would appreciate some clarification.

I have

use TEMPLATE

in my code to control whether the attribute

selected

appears with one of the < options> of the list following <select> in my served html document. Here’s one line from that list

<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? I can try them out to see what will work (perhaps all will work), but I want to know what is considered best practice and why.

Perhaps there’s even a whole better way to approach this! I will be grateful to chew over any and all comments you experienced people send me. Thanks!

Rick Triplett
Shlomi Fish (03-14-19, 11:40 PM)
Hi Rick,

On Thu, 14 Mar 2019 12:14:59 -0500
Rick T <perl> wrote:

> I’m sure this is a “newbie question,” but I am only a shade above that status
> and would appreciate some clarification.
> I have
> use TEMPLATE


do you mean ?
> in my code to control whether the attribute
> selected
> appears with one of the < options> of the list following <select> in my
> served html document. Here’s one line from that list
> <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? I can
> try them out to see what will work (perhaps all will work), but I want to
> know what is considered best practice and why.


an empty string should be fine.
David Precious (03-19-19, 02:44 PM)
On Thu, 14 Mar 2019 12:14:59 -0500
Rick T <perl> 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="[% course.id %]"
[% IF course.selected %]selected[% END %]>[% course.name %]
</option>
[% 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.

Cheers

Dave P
Similar Threads