user1462804
user1462804

Reputation: 33

Switch Case statements in template toolkit

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...

Upvotes: 0

Views: 1196

Answers (1)

RET
RET

Reputation: 9188

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" -%]

Upvotes: 2

Related Questions