Search code examples
phpsmarty

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?


Solution

  • 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