Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Increment and decrement

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Fri May 02, 2003 3:56 am    Post subject: Increment and decrement Reply with quote

I have found that most of the time what I want to use (but have sucessfully avoided Smile ) {math} for is to look for "one less than the end of the array" or some such.

Last night I had an epiphany: make ++ and -- modifiers! Here they are, they work great:

modifier.pp.php
[php:1:66d15d0533]<?php
/*
* Smarty plugin
*
-------------------------------------------------------------
* File: modifier.pp.php
* Type: modifier
* Name: pp
* Version: 1.0
* Date: May 1st, 2003
* Purpose: assume the input string is a number, increment and return result
* Install: Drop into the plugin directory.
* Author: Jason E. Sweat <jsweat_php@yahoo.com>
*
-------------------------------------------------------------
*/
function smarty_modifier_pp($string)
{
return ++$string;
}

/* vim: set expandtab: */

?>
[/php:1:66d15d0533]

and modifier.mm.php
[php:1:66d15d0533]<?php
/*
* Smarty plugin
*
-------------------------------------------------------------
* File: modifier.mm.php
* Type: modifier
* Name: mm
* Version: 1.0
* Date: May 1st, 2003
* Purpose: assume the input string is a number, decrement and return result
* Install: Drop into the plugin directory.
* Author: Jason E. Sweat <jsweat_php@yahoo.com>
*
-------------------------------------------------------------
*/
function smarty_modifier_mm($string)
{
return --$string;
}

/* vim: set expandtab: */

?>
[/php:1:66d15d0533]

Here is an example portion of a template that uses them:

Code:
<tr>
  <td colspan="3" align="center">
  {assign var=gid value=$link[l].link_group_id}
  <a href="{$action_link}?{$action}=DelLink&lid={$lid}&gid={$gid}">Delete</a>
  {if $link|@count gt 1}
    <br />
    {if !$smarty.section.l.first}
      <a href="{$action_link}?{$action}=OrdLink&lid={$lid}&ord=1&gid={$gid}">Top</a>
      {if $smarty.section.l.index gt 1}
        <a href="{$action_link}?{$action}=OrdLink&lid={$lid}&ord={$link[l].link_ord|mm}&gid={$gid}">Up</a>
      {/if}
    {/if}
    {if !$smarty.section.l.last}
      {if $smarty.section.l.rownum lt $link|@count|mm}
        <a href="{$action_link}?{$action}=OrdLink&lid={$lid}&ord={$link[l].link_ord|pp}&gid={$gid}">Down</a>
      {/if}
      <a href="{$action_link}?{$action}=OrdLink&lid={$lid}&ord={$link|@count}&gid={$gid}">Bottom</a>
    {/if}
  {/if}
  </td>
</tr>

_________________
Jason
jsweat_php AT yahoo DOT com
Back to top
View user's profile Send private message
joscha
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Metzingen, Germany

PostPosted: Fri May 02, 2003 9:22 am    Post subject: Reply with quote

Nice Idea, only a small plugin, but gets rid of the overhead of the math plugin...however I changed it a bit (I don't like having too much different plugins for the same type of work, it fills the plugin namespace too much and is hard to understand for my graphics people Smile
So my version of your plugin is here:
[php:1:b251d1214e]<?php
/*
* Smarty plugin
*
-------------------------------------------------------------
* File: modifier.id.php
* Type: modifier
* Name: id
* Version: 1.0
* Date: 2003-05-02
* Purpose: assume the input string is a number, increment/decrement and return result
* Install: Drop into the plugin directory.
* Params: mixed number the number to alter, needed
* string mode (enum) "i" for increment, "d" for decrement, default is "i"
* mixed alteration the alteration to use, default is 1
* Usage: {$myvar|id:"i"} increments $myvar by one
* {$myvar|id:"d"} decrements $myvar by one
* {$myvar|id:"i":3} increments $myvar by 3
*
* Author: Jason E. Sweat <jsweat_php@yahoo.com>
* Joscha Feth <joscha@feth.com>
*
-------------------------------------------------------------
*/
function smarty_modifier_pp($number, $mode = 'i', $alteration = 1)
{
if($mode == "i") {
return $number += $alteration;
} else {
return $number -= $alteration;
}
}
?>[/php:1:b251d1214e]

greets,
Joscha
Back to top
View user's profile Send private message Visit poster's website
rosier
Smarty n00b


Joined: 20 Apr 2003
Posts: 2
Location: NL

PostPosted: Fri May 02, 2003 12:12 pm    Post subject: Reply with quote

Nice Smile, but I think the following might be easier for an designer or not... Anyway here goes...

[php:1:bf5cd998c1]<?php
/*
* Smarty plugin
*
-------------------------------------------------------------
* Purpose: assume the input string is a number, increment/decrement and return result
* Install: Drop into the plugin directory.
* Params: mixed number the number to alter, needed
* mixed alteration the alteration to use, default is 1
*
* Usage: {$myvar|increment} increments $myvar by one
* {$myvar|increment:-1} decrements $myvar by one
* {$myvar|increment:3} increments $myvar by 3
*
-------------------------------------------------------------
*/
function smarty_modifier_increment($number, $alteration = 1)
{
return $number += $alteration;
}
?>[/php:1:bf5cd998c1]
Back to top
View user's profile Send private message
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Fri May 02, 2003 2:24 pm    Post subject: Reply with quote

I like Rosier version better, but I would name it "add"

{$var|add}
{$var|add:-1}
{$var|add:3}
_________________
Jason
jsweat_php AT yahoo DOT com
Back to top
View user's profile Send private message
brettz9
Smarty Regular


Joined: 07 Jul 2006
Posts: 93

PostPosted: Thu Jul 13, 2006 10:42 am    Post subject: Reply with quote

Is there no way to pass variables by reference to a modifier? It would seem the usefulness of this function would be greatly expanded if this were possible. I tried adding "&" to the $number parameter to no effect.

(Note a little bug in joscha's if you want to use that one: "smarty_modifier_pp" should be "smarty_modifier_id")
Back to top
View user's profile Send private message
brettz9
Smarty Regular


Joined: 07 Jul 2006
Posts: 93

PostPosted: Thu Jul 13, 2006 10:52 am    Post subject: Reply with quote

I see now that one can just do: {$my_var++}. The documentation didn't mention this as one of the math features...
Back to top
View user's profile Send private message
weckie
Smarty Rookie


Joined: 11 Sep 2005
Posts: 10

PostPosted: Fri Jul 28, 2006 11:45 pm    Post subject: Reply with quote

i need a math function too, for you guys it is very easy i think.

i need a calculation within an {section}

The output must be in a drop down selection list:

5
10
15
20 and so on..

Anyone knows how to do this.
_________________
Regards,

Weckie
www.weckonline.nl
Using Smarty in comb. with X-cart.
Back to top
View user's profile Send private message
brettz9
Smarty Regular


Joined: 07 Jul 2006
Posts: 93

PostPosted: Sat Jul 29, 2006 2:38 am    Post subject: Reply with quote

The simplest solution would probably be to use "counter". There would be different ways to do this depending on your desired outcome and your preferences for appearance/functioning inside of the template.

If you had to have the same value reused (e.g., within the value as well as the name in the drop down) and you wanted to print out both, you could do:
Code:

<option value="{counter assign=optcount start=5 skip=5}">{$optcount}</option>


If you only used it in one place, you could do:

Code:
<option value="">{counter start=5 skip=5}</option>


If you want two independent counters (probably only needed if you wanted different values between the two), you'll need to add a name to the counter. See the documentation at http://smarty.php.net/manual/en/language.function.counter.php

Assign could also be used for this (e.g., {assign var=level value=$level+5} ), but it is probably more clear to use the specifically designed "counter" and "assign" does not print itself out.

For future reference, the Math function can also do math, but is not needed here.
Back to top
View user's profile Send private message
weckie
Smarty Rookie


Joined: 11 Sep 2005
Posts: 10

PostPosted: Sat Jul 29, 2006 7:04 am    Post subject: Reply with quote

Hi Brettz,

Thank for your answer. BUT..

I work with X-cart and i need it as orderamount.

Start amount is for example 5, but this 5 is coming from xcart as {$order_minamount} this variable can differ per produkt.

Code:
{if $config.General.unlimited_products eq "Y"}
{assign var="mq" value=$config.Appearance.max_select_quantity}
{else}
{math equation="x/y" x=$config.Appearance.max_select_quantity y=$product.min_amount assign="tmp"}
{if $tmp<2}
{assign var="minamount" value=$product.min_amount}
{else}
{assign var="minamount" value=1}
{/if}
{math equation="min(maxquantity+minamount, productquantity+1)" assign="mq" maxquantity=$config.Appearance.max_select_quantity minamount=$minamount productquantity=$product.avail}
{/if}
{if $product.min_amount le 1}
{assign var="start_quantity" value=1}
{else}
{assign var="start_quantity" value=$product.min_amount}
{/if}

{if $config.General.unlimited_products eq "Y"}
{math equation="x+y" assign="mq" x=$mq y=$start_quantity}
{/if}
<select name="amount">
{section name=quantity loop=$mq start=$start_quantity}
   <option value="{%quantity.index%}"{if $smarty.get.quantity eq %quantity.index%} selected="selected"{/if}>{%quantity.index%}


</option>
{/section}
</select>
{/if}


this is the code i need it in....
Bij default xcart starts with the minimum orderamount as set the productspecifications. for example 5 then the output is 5, 6, 7, etc...

I hope you understand what i mean...
_________________
Regards,

Weckie
www.weckonline.nl
Using Smarty in comb. with X-cart.
Back to top
View user's profile Send private message
anshumanravi
Smarty n00b


Joined: 09 Sep 2010
Posts: 1

PostPosted: Thu Sep 09, 2010 1:05 pm    Post subject: Incrementing variable Reply with quote

Use this :
<span style="display:none">{$smarty_var++}</span>

using hidden will not print the variable but increment it only... Cool
Back to top
View user's profile Send private message Send e-mail
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7237
Location: Lincoln Nebraska, USA

PostPosted: Thu Sep 09, 2010 1:43 pm    Post subject: Re: Incrementing variable Reply with quote

anshumanravi wrote:
Use this :
<span style="display:none">{$smarty_var++}</span>

using hidden will not print the variable but increment it only... Cool


Another way without html output:

Code:
{if $smarty_var++}{/if}
Back to top
View user's profile Send private message Visit poster's website
khhamsin
Smarty n00b


Joined: 24 Nov 2011
Posts: 1

PostPosted: Thu Nov 24, 2011 8:29 am    Post subject: Reply with quote

I reassign variables:

{assign var=$i value=0}
{foreach... }
(do something here...)
{assign var=$i value=$i++}
{/foreach}
Back to top
View user's profile Send private message
plumbingservices02
Smarty n00b


Joined: 25 Nov 2011
Posts: 1

PostPosted: Fri Nov 25, 2011 11:11 am    Post subject: Reply with quote

Excellent work.By saying this you have hit the head of the nail.cheers
Kearny Plumbing
Plumbing Dallas TX
Plumbing Lake Worth FL
Plumbing Sunrise Rolling Eyes
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP