I want to add a constant value to a variable in smarty. just like:
{assign var='c' value='0'}
$c=$c+1
Variables in Smarty can be either displayed directly or used as arguments for functions, attributes and modifiers, inside conditional expressions, etc. To print a variable, simply enclose it in the delimiters so that it is the only thing contained between them. Example 4.1. Example variables.
{capture} is used to collect the output of the template between the tags into a variable instead of displaying it. Any content between {capture name='foo'} and {/capture} is collected into the variable specified in the name attribute. The captured content can be used in the template from the variable $smarty.
Try this:
{assign var='c' value=0}
{assign var='c' value=$c+1}
The short form should work too, but you say it doesn't.
{$c=0}
{$c=$c+1}
But this doesn't work because you're using Smarty 2, right? Because in Smarty 3 it should work.
Try:
{assign var="c" value="`$something+$constant`"}
But usually the sense of template frameworks is to follow the mvc pattern. So all the logic is done in a controller. Or in the case of you some sort of php script. The view should not hold much logic(less logic better view code). So any sort of calculations should not be in a view. In mvc you will have however some sort of logic like iterations or link generation(through e.g. smarty plugins).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With