0

I want to implement switch case statement in Template toolkit. My code is as follows:

[% SWITCH myvar %]
[% CASE > 4 %]
Value is amplified
[% CASE < 1%]
Value is Deleted
[% CASE %]
Normal Value
[%END%]

I am getting a error message saying '<' and '>' are unexpected tokens in my script. Can Any one help me resolve this issue. I preferably dont want to use IF statements as it is reducing the speed of execution of my script. Is there any other alternative for the above.

Thanks in advance...

1 Answers1

2

Template code does not support anything other than equality or in-list, as explained in the fine manual.

Having said that, I would be extraordinarily surprised if a CASE statement compiled down to something that executed faster than IF ... ELSIF ... END. In fact, I'd put money on either syntax compiling down to the exact same thing. You could also write this as a sequence of ternary operators, but I still think it would make no difference, speed-wise.

[%- IF myvar > 4;
        "Value is amplified";
    ELSIF myvar < 1;
        "Value is Deleted";
    ELSE;
        "Normal Value";
    END; -%]

...or...

[%- (myvar > 4) ? "Value is amplified" :
    (myvar < 1) ? "Value is Deleted" : "Normal Value" -%]
RET
  • 9,100
  • 1
  • 28
  • 33
  • Thanks for your reply once again... I had already implemented IF statement in my code, but I wanted to see whether Switch case would have anything to do with speed.. – user1462804 Aug 24 '12 at 04:07
  • Glad to help. Please consider ticking the answer and/or upvoting it. That's how StackOverflow works. – RET Aug 24 '12 at 04:33