Smarty - adding a variable

I want to add a constant variable value to smarty. as:

{assign var='c' value='0'} $c=$c+1 
+6
source share
3 answers

Try the following:

 {assign var='c' value=0} {assign var='c' value=$c+1} 

The short form should also work, but you say that it is not.

 {$c=0} {$c=$c+1} 

But this does not work because you are using Smarty 2, right? Because in Smarty 3 it should work.

+11
source

Try:

 {assign var="c" value="`$something+$constant`"} 

But usually the point of template frameworks is to follow the mvc pattern. So, all the logic is executed in the controller. Or in the case of you some php script. The presentation should not have much logic (less logical code of the best representation). Therefore, any calculations should not be considered. In mvc, you will have some kind of logic, such as iterating or generating links (e.g. smarty plugins).

+3
source

You can use expressions with the {assign} function.

 {assign var=c value=$c+1} 

Or in his short form

 {$c=$c+1} 
+3
source

Source: https://habr.com/ru/post/893415/


All Articles