45

So I have an assigned variable in smarty:

{assign var=number value=0}

Now I can increment it using

{$number++}

or

{++$number}

Which is exactly what I need, only problem is, it displays the value of $number on the page. Is there a way I can increment the value but not display it?

This is not used inside of a loop otherwise I would use something like iteration or index.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65

4 Answers4

82

You could do this:

{assign var=val value=1}
{assign var=val value=$val+1}
{$val} // displays 2

The above will be compiled to:

$this->assign('val', 1);
$this->assign('val', $this->_tpl_vars['val']+1);
echo $this->_tpl_vars['val'];

or

{assign var=var value=1}
{capture assign=var}{$var+1}{/capture}
{$var} // displays 2

Which in turn will be compiled as:

$this->assign('var', 1);
ob_start();
echo $this->_tpl_vars['var']+1;
$this->_smarty_vars['capture']['default'] = ob_get_contents();
$this->assign('var', ob_get_contents());
ob_end_clean();
echo $this->_tpl_vars['var'];

another approach would be to write a small plugin:

// plugins/function.inc.php
function smarty_function_inc($params, Smarty &$smarty)
{
   $params['step'] = empty($params['step']) ? 1 : intval($params['step']);

   if (empty($params['var'])) {
      trigger_error("inc: missing 'var' parameter");
      return;
   }
   if (!in_array($params['var'], array_keys($smarty->_tpl_vars))) {
      trigger_error("inc: trying to increment unassigned variable " . $params['var']);
      return;
   }
   if (isset($smarty->_tpl_vars[$params['var']])) {
      $smarty->assign($params['var'],
      $smarty->_tpl_vars[$params['var']] + $params['step']);
   }
}

The function would then be called like this, notice that step is optional and if not given the variable will be incremented by one:

{assign var=var value=0}
{inc var=var step=2}
{$var} // displays 2

Reference
Smarty {assign}
Smarty {capture}
Extending Smarty With Plugins

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
19

It's cleaner just to do it like this...

{$number = $number +1}
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
trapper
  • 11,716
  • 7
  • 38
  • 82
5

Better to use the built in Smarty "counter" >> {counter} element.

So, in template you can use:

<div>Some text, html code, whatever... </div>
{* init the counter! *}
{counter start=0 print=false} {* now the tpl doesn't show the "0" number *}


{* 3x run :D *}
{some_cyclic_stuff_like_foreach_or_section}
    Run the counter: {counter}
{/some_cyclic_stuff_like_foreach_or_section}

It will print for you:

Run the counter: 1
Run the counter: 2
Run the counter: 3

So, at least, you can use it with the print=false option, and you have the counter but it's hidden.

If you use it the "variable way" (like the upper section write) you also can hide it with html/css or just simply don't let it print :)

huncyrus
  • 648
  • 8
  • 18
-1

If I had to do this I would do something like the following: {php}$number++{/php} but it is very ugly to have to use php in a smarty template. This might suggest that there is a better way of doing what you planned to do.

ridecar2
  • 1,968
  • 16
  • 34
  • I thought of this as well and got it working very nicely with PHP, but I wasn't sure how to increment a variable assigned through smarty, so I ended up using php in several places throughout the template to assign the variable, then increment it as needed, then later on to actually use it - it got kind of ugly, although I'll drop back to that if I don't have any better solutions. – Francis Lewis Dec 29 '11 at 23:58
  • 2
    Is there a reason that you couldn't do all the legwork in the php and assign a load of variables/arrays? What you want to do is a lot of work for the template and completely defeats the purpose of using smarty. – ridecar2 Dec 29 '11 at 23:59
  • 1
    One bit of information I forgot to put in here is that I'm using a while loop in the template. After your latest comment, I came to the realization that I could increment the number in the {while} tag {while $number++ < 3} - headpalm moment. Thanks! – Francis Lewis Dec 30 '11 at 00:45
  • I wanted to increment the array index by 1 as it starts with 0 in most languages. This is how I did in Smarty and eZPublish `{$index|inc(1)}`. Now the first element index shows 1 instead of 0 in the printed table. – rashidkhan Mar 01 '23 at 23:23