Connecting Tech Pros Worldwide Forums | Help | Site Map

Addition or Subtraction in smarty

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#1: Dec 8 '08
Hey guys,

For the love of all things holy I can't figure out how to add two numbers together in smarty. I've checked the manual, there's no main section, if it's in there, it's probably lost within some section.

I've tried {2+3}, {(2+3)}.

What i'm trying to do is add a number of days to a date. I have a list of objects, and a member of the object is a date (start date). These "things" expire within 45 days, so what I need to do is show the expiration date.

My object does not have a expireDate member and I don't want to change my array of objects to an array of array and add the expiration date on the PHP side, too resource intensive.

Any clue?

Here's the TPL code:

Expand|Select|Wrap|Line Numbers
  1. <ol>
  2.                         {foreach from=$resultVOList item="postVO"}
  3.                             <li><a href="#" class="postLink">{$postVO->title}</a> &mdash; {$postVO->location} &mdash; <b> ${$postVO->price} </b>
  4.                             <br /><b>Author:</b> {$postVO->author}
  5.                             <br /><b>ISBN:</b> {$postVO->ISBN}
  6.                             <br /><b>Created:</b> {$postVO->datePosted|date_format}
  7.                             <br /><b>Expires:</b> {2+3}
  8.                             <div class="right"> <a href="#">[ Edit ]</a> - <a href="#">[ Delete ]</a></div>
  9.                             <div class="hrzln" ></div></li>    
  10.                         {foreachelse}
  11.                             <li>No Book Posts, click on New Post to add a book for sale</li>
  12.                         {/foreach}
  13.  
  14.                     </ol>
Thanks everyone,


Dan

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Dec 8 '08

re: Addition or Subtraction in smarty


Couldn't find it, so I added a modified for Smarty here's the code if anybody wants it:

modifier.addDays.php (put this in plugin dir)

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. /**
  4.  * Smarty plugin
  5.  * @package Smarty
  6.  * @subpackage plugins
  7.  */
  8.  
  9.  
  10. /**
  11.  * Smarty custom plugin to add a integer to a date and returns the date in US format mm/dd/yyyy
  12.  *
  13.  * Type:     modifier<br>
  14.  * Name:     addDays<br>
  15.  * Purpose:  adds a specified number days to a date
  16.  * @author   Dana Murad
  17.  * @param string
  18.  * @param integer
  19.  * @return string
  20.  */
  21. function smarty_modifier_addDays($string,$numOfDays)
  22. {
  23.     list($month,$day,$year) = explode("/",$string); 
  24.     $date = mktime(0,0,0,$month,$day+$numOfDays,$year);
  25.     return date("m/d/Y",$date); 
  26. }
  27.  
  28. ?>
  29.  
  30.  
  31.  
so my code now looks like:

Expand|Select|Wrap|Line Numbers
  1.                     <ol>
  2.                         {foreach from=$resultVOList item="postVO"}
  3.                             <li><a href="#" class="postLink">{$postVO->title}</a> &mdash; {$postVO->location} &mdash; <b> ${$postVO->price} </b>
  4.                             <br /><b>Author:</b> {$postVO->author}
  5.                             <br /><b>ISBN:</b> {$postVO->ISBN}
  6.                             <br /><b>Created:</b> {$postVO->datePosted|date_format}
  7.                             <br /><b>Expires:</b> {$postVO->datePosted|addDays:$smarty.const.EXPIRE_DAYS|date_format}
  8.                             <div class="right"> <a href="#">[ Edit ]</a> - <a href="#">[ Delete ]</a></div>
  9.                             <div class="hrzln" ></div></li>    
  10.                         {foreachelse}
  11.                             <li>No Book Posts, click on New Post to add a book for sale</li>
  12.                         {/foreach}
  13.  
  14.                     </ol>    
  15.  
Hope it helps somebody, Suggestions welcome,





Dan
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#3: Dec 8 '08

re: Addition or Subtraction in smarty


Wow, that seems strange that they wouldn't have something so simple like {2+3}.. I had a scan around for you, and found something called 'math equation'. I think you do it like so: {math equation="1 + 1"}. I'm sure you'll be able to find something about it on google.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#4: Dec 9 '08

re: Addition or Subtraction in smarty


Thanks Markus.

I'd like to point out that I found something interesting about the addition (and I assume other operations).

Whitespace matters. For example: {$num1+$num2} works but {$num1 + $num2} does not.

The plus sign has to be attached to the numbers your adding or else you'll get an error or only the first number is shown.

Since most developers code with PHP and HTML, thus disregard whitespace, I just wanted to point that out.




Dan
Reply