How to execute mod (%) operation in smarty?

I am new to Smarty. I want to do a mod operation on an array element in smarty. The following is a snippet of the code I'm trying to implement:

{if {$que_seq_no}.{$sub_ques_no+1} % 10 == 1} 

Could you help me fix this? Thanks at Advance.

+4
source share
2 answers

Answer to the question

The operator is modulo % and also has an alternation mod .
More about recognized qualifiers in smarty reffer for documents: http://www.smarty.net/docsv2/en/language.function.if

Answer your problem

So, as you now know, your modulo statement is correct.
From what I can guess from your syntax, you are probably trying to access the value of the array by the index of the array.

To access the elements of the array you have 2 options:

  • Array Access - Syntax: $arrayVariable.key
  • Access by array index - syntax: $arrayVariable[index] (index - int number)
    So, in your case: $que_seq_no[$sub_ques_no+1]

If you are trying to access an object:
it looks like access by array key, but the operator -> , so the syntax is: $objectVariable->propertyName

Refer to smart documents for more on this: http://www.smarty.net/docsv2/en/language.variables.tpl

Which probably confused you

I think you are a bit confused about using {} (braces)
- they are used to wrap the expression of the whole smarty, so it follows from it that they cannot contain any other { or } .
Nice example taken from smarty docs:

 {if $name == 'Fred' || $name == 'Wilma'} ... {/if} 
+2
source

You use acceptable syntax for the mod operation. I am sure that you are using the wrong syntax with the variable {$que_seq_no}.{$sub_ques_no+1} . Have you tried $que_seq_no[$sub_ques_no+1] ?

+2
source

All Articles