Skip to content
Advertisement

How to assign an expression to a variable and check if it is odd or even in Smarty Script

In smarty script I need to assign a random integer to a variable and display some content if that variable is odd or even. I know I can get a random integer with

{math equation = rand(1,20)}

But how to assign it to a variable and check if it is odd or even?

Advertisement

Answer

To assign to a variable in Smarty you can use

{$check=rand(1,20) nocache}

https://www.smarty.net/docs/en/language.builtin.functions.tpl#language.function.shortform.assign

To check if a variable is even you can use

{if $check is even}
   ...
{/if}

https://www.smarty.net/docs/en/language.function.if.tpl

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement