9

So it seems that if I give template toolkit a reference to an array as a parameter

ARRAY_REF => \@array

and then have the following code in a template

[% IF ( ARRAY_REF ) %]
  Do something
[% ELSE %]
  Do something else
[% END %]

The else case never gets triggered.

Replacing the parameter code with

ARRAY_REF => @array ? \@array : undef;

seems to solve the issue, however I was wondering if there is a way to make template toolkit evaluate an empty array (passed via reference) as false as there are many instances throughout my project where I believe this is being used (as in HTML template pro it worked as expected).

Thank you all in advance for your assistance.

cjh
  • 1,113
  • 1
  • 9
  • 21

3 Answers3

24

Your ARRAY_REF will be true because it is defined and it would be a true value in Perl. The usual approach is to check that it is true and non-empty:

[% IF ARRAY_REF && ARRAY_REF.size %]
    Do something
[% ELSE %]
    Do something else
[% END %]

Say what you really mean, asking the computer to pretend to be smarter than it is leads to odd surprises.

You could probably change TT's notion of truthiness but I don't think you'd enjoy it or the various unpleasant side effects you'd probably come across. Template Toolkit isn't HTML Template Pro, when in Rome do as the Romans do and all that.

Your best bet is to fix your templates and consider the extra work as just part of the porting process. You could probably build a plugin to do the "true and non-empty" stuff for you though.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • If the first test necessary? It seems just doing [% IF ARRAY_REF.size %] doesn't complain when there is no such thing as ARRAY_REF. – cjh Feb 03 '11 at 21:18
  • @suicideducky: Just `[% IF ARRAY_REF.size %]` should be sufficient. Checking if it is there before calling `.size` on it is a habit from all the other languages I work with. – mu is too short Feb 03 '11 at 21:56
  • @mu I am a C++ programmer myself so I have the same habit, just when I was mucking around I noticed that perl didn't seem to care so I thought I would ask. Cheers. – cjh Feb 03 '11 at 22:13
  • @suicideducky: **Perl does care**, Template Toolkit doesn't care, C cares, C++ cares, JavaScript cares, Ruby cares, ... so I care too to make it easier to keep everything straight. – mu is too short Feb 04 '11 at 00:01
  • @mu forgive me if I'm misstaken, but the following:"my $a; if($a.badsd){print "a.badsd\n";}; if($b.dudsa){print "b.dudsa\n";};" seems to work fine, printing both the print statements. Were you talking about perl with a use strict/warnings combo? – cjh Feb 04 '11 at 02:00
  • 1
    @suicideducky: "my $a; if($a->badsd){print "a->badsd\n";}; if($b->dudsa){print "b->dudsa\n";};", `.` is for string concatenation (like `_` in Template Toolkit or `+` in JavaScript or C++) whereas `->` is like it is in C++. And, yes, I always `use strict;use warnings;` even if it is just a quick one-off hack, just like I always `-Wall -Werror` with gcc when working in C or C++. – mu is too short Feb 04 '11 at 02:54
  • @mu Silly mistake on my part, can't believe I used the wrong operator. Thank you very much for taking the time to clear that up :) – cjh Feb 06 '11 at 21:05
  • @suicideducky: No worries, my usual operator mistake is confusing "." and "_" in Perl while working with TT. – mu is too short Feb 07 '11 at 03:33
11

I think .size is what you want.

perl -MTemplate -le '$t = Template->new; $t->process(\"[% \"O HAI\" IF arrayref.size %]", { arrayref => [] })'

perl -MTemplate -le '$t = Template->new; $t->process(\"[% \"O HAI\" IF arrayref.size %]", { arrayref => [1] })'
O HAI

I'd also offer that an empty array ref is true in plain Perl–

perl -le '$abc = []; print "true" if $abc'
true

And when you do it directly it's more obvious (maybe) why it should be obvious–

perl -le 'print "true" if []'
true
Ashley
  • 4,307
  • 2
  • 19
  • 28
  • 2
    Also correct, but sadly I cannot accept two answers. Thank you very much though :) – cjh Feb 03 '11 at 21:16
-3

Test by accessing the 1st array element:

[% IF ( ARRAY_REF.0 ) %]
tadmc
  • 3,714
  • 16
  • 14