Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smarty - variable addition

Tags:

php

smarty

I want to add a constant value to a variable in smarty. just like:

{assign var='c' value='0'}
$c=$c+1
like image 801
RSK Avatar asked Jul 23 '11 15:07

RSK


People also ask

What is Smarty variable?

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.

What is capture in Smarty?

{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.


2 Answers

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.

like image 170
duality_ Avatar answered Nov 15 '22 21:11

duality_


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).

like image 37
fyr Avatar answered Nov 15 '22 22:11

fyr