proper use of TEMPLATE

I=E2=80=99m sure this is a =E2=80=9Cnewbie question,=E2=80=9D 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=20

selected=20

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

<option value=3D"1" [% bool_full_selected %]> Full Course (1 =
credit)</option>

My code can easily replace the correct var with =E2=80=98selected=E2=80=99=
 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=E2=80=99s 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=
0
perl
3/14/2019 5:14:59 PM
perl.beginners 29332 articles. 3 followers. Follow

3 Replies
9 Views

Similar Articles

[PageSpeed] 58

Hi Rick,

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

> I=E2=80=99m sure this is a =E2=80=9Cnewbie question,=E2=80=9D but I am on=
ly a shade above that status
> and would appreciate some clarification.
>=20
> I have
>=20
> use TEMPLATE
>=20

do you mean https://metacpan.org/pod/Template ?
> in my code to control whether the attribute=20
>=20
> selected=20
>=20
> appears with one of the < options> of the list following <select> in my
> served html document. Here=E2=80=99s one line from that list
>=20
> <option value=3D"1" [% bool_full_selected %]> Full Course (1 credit)</opt=
ion>
>=20
> My code can easily replace the correct var with =E2=80=98selected=E2=80=
=99 and the other vars
> on the list with a blank. But what should that blank be?
>=20
> 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.
>=20

an empty string should be fine.

> Perhaps there=E2=80=99s 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!
>=20
> Rick Triplett



--=20
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Parody of "The Fountainhead" - http://shlom.in/towtf

I=E2=80=99d give my right arm to be ambidextrous.
    =E2=80=94 Source unknown via Nadav Har=E2=80=99El and fortune-mod

Please reply to list if it's a mailing list post - http://shlom.in/reply .
0
shlomif
3/14/2019 9:40:05 PM
UmljaywNCg0KQ2FuIHlvdSBpbmNsdWRlIHRoZSBhY3R1YWwgY29kZSB5b3UgYXJlIHJlZmVycmlu
ZyB0byBhcyBmcm9tIHlvdXIgbWVzc2FnZSBJIGRvIG5vdCB5ZXQgdW5kZXJzdGFuZCB3aGF0IHRo
ZSBpc3N1ZSBpcw0KDQogIER1bmNzDQoNCi0tLS0tT3JpZ2luYWwgTWVzc2FnZS0tLS0tDQpGcm9t
OiBSaWNrIFQgW21haWx0bzpwZXJsQHJlYXNvbi5uZXRdIA0KU2VudDogMTQgTWFyY2ggMjAxOSAx
NzoxNQ0KVG86IFBlcmwgQmVnaW5uZXJzIDxiZWdpbm5lcnNAcGVybC5vcmc+DQpTdWJqZWN0OiBw
cm9wZXIgdXNlIG9mIFRFTVBMQVRFDQoNCknigJltIHN1cmUgdGhpcyBpcyBhIOKAnG5ld2JpZSBx
dWVzdGlvbizigJ0gYnV0IEkgYW0gb25seSBhIHNoYWRlIGFib3ZlIHRoYXQgc3RhdHVzIGFuZCB3
b3VsZCBhcHByZWNpYXRlIHNvbWUgY2xhcmlmaWNhdGlvbi4NCg0KSSBoYXZlDQoNCnVzZSBURU1Q
TEFURQ0KDQppbiBteSBjb2RlIHRvIGNvbnRyb2wgd2hldGhlciB0aGUgYXR0cmlidXRlIA0KDQpz
ZWxlY3RlZCANCg0KYXBwZWFycyB3aXRoIG9uZSBvZiB0aGUgPCBvcHRpb25zPiBvZiB0aGUgbGlz
dCBmb2xsb3dpbmcgPHNlbGVjdD4gaW4gbXkgc2VydmVkIGh0bWwgZG9jdW1lbnQuIEhlcmXigJlz
IG9uZSBsaW5lIGZyb20gdGhhdCBsaXN0DQoNCjxvcHRpb24gdmFsdWU9IjEiIFslIGJvb2xfZnVs
bF9zZWxlY3RlZCAlXT4gRnVsbCBDb3Vyc2UgKDEgY3JlZGl0KTwvb3B0aW9uPg0KDQpNeSBjb2Rl
IGNhbiBlYXNpbHkgcmVwbGFjZSB0aGUgY29ycmVjdCB2YXIgd2l0aCDigJhzZWxlY3RlZOKAmSBh
bmQgdGhlIG90aGVyIHZhcnMgb24gdGhlIGxpc3Qgd2l0aCBhIGJsYW5rLiBCdXQgd2hhdCBzaG91
bGQgdGhhdCBibGFuayBiZT8NCg0KU2hvdWxkIEkgbWFrZSBpdCBhbiBlbXB0eSBzdHJpbmcsIGEg
c3BhY2UsIHVuZGVmLCBvciBzb21ldGhpbmcgZWxzZT8gSSBjYW4gdHJ5IHRoZW0gb3V0IHRvIHNl
ZSB3aGF0IHdpbGwgd29yayAocGVyaGFwcyBhbGwgd2lsbCB3b3JrKSwgYnV0IEkgd2FudCB0byBr
bm93IHdoYXQgaXMgY29uc2lkZXJlZCBiZXN0IHByYWN0aWNlIGFuZCB3aHkuDQoNClBlcmhhcHMg
dGhlcmXigJlzIGV2ZW4gYSB3aG9sZSBiZXR0ZXIgd2F5IHRvIGFwcHJvYWNoIHRoaXMhIEkgd2ls
bCBiZSBncmF0ZWZ1bCB0byBjaGV3IG92ZXIgYW55IGFuZCBhbGwgY29tbWVudHMgeW91IGV4cGVy
aWVuY2VkIHBlb3BsZSBzZW5kIG1lLiBUaGFua3MhDQoNClJpY2sgVHJpcGxldHQNCi0tIA0KVG8g
dW5zdWJzY3JpYmUsIGUtbWFpbDogYmVnaW5uZXJzLXVuc3Vic2NyaWJlQHBlcmwub3JnDQpGb3Ig
YWRkaXRpb25hbCBjb21tYW5kcywgZS1tYWlsOiBiZWdpbm5lcnMtaGVscEBwZXJsLm9yZw0KaHR0
cDovL2xlYXJuLnBlcmwub3JnLw0KDQoNCg==
0
duncan
3/15/2019 9:28:35 AM
On Thu, 14 Mar 2019 12:14:59 -0500
Rick T <perl@reason.net> wrote:
> <option value=3D"1" [% bool_full_selected %]> Full Course (1
> credit)</option>
>=20
> My code can easily replace the correct var with =E2=80=98selected=E2=80=
=99 and the
> other vars on the list with a blank. But what should that blank be?
>=20
> 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=3D"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=3D"" attribute, the course
name to display, etc... something like:

  my %courses =3D (
    { id =3D> 1, name =3D> "Full Course", selected =3D> 1 },
    { id =3D> 2, name =3D> "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=3D"[% 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
0
davidp
3/19/2019 12:44:13 PM
Reply: