Addition or Subtraction in smarty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    Addition or Subtraction in smarty

    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:

    Code:
    <ol>
    						{foreach from=$resultVOList item="postVO"}
    							<li><a href="#" class="postLink">{$postVO->title}</a> &mdash; {$postVO->location} &mdash; <b> ${$postVO->price} </b>
    							<br /><b>Author:</b> {$postVO->author}
    							<br /><b>ISBN:</b> {$postVO->ISBN}
    							<br /><b>Created:</b> {$postVO->datePosted|date_format}
    							<br /><b>Expires:</b> {2+3}
    							<div class="right"> <a href="#">[ Edit ]</a> - <a href="#">[ Delete ]</a></div>
    							<div class="hrzln" ></div></li>	
    						{foreachelse}
    							<li>No Book Posts, click on New Post to add a book for sale</li>
    						{/foreach}
    	
    					</ol>
    Thanks everyone,


    Dan
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

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

    modifier.addDay s.php (put this in plugin dir)

    Code:
    <?php
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */
    
    
    /**
     * Smarty custom plugin to add a integer to a date and returns the date in US format mm/dd/yyyy
     *
     * Type:     modifier<br>
     * Name:     addDays<br>
     * Purpose:  adds a specified number days to a date
     * @author   Dana Murad
     * @param string
     * @param integer
     * @return string
     */
    function smarty_modifier_addDays($string,$numOfDays)
    {
    	list($month,$day,$year) = explode("/",$string); 
    	$date = mktime(0,0,0,$month,$day+$numOfDays,$year);
        return date("m/d/Y",$date); 
    }
    
    ?>
    so my code now looks like:

    Code:
    					<ol>
    						{foreach from=$resultVOList item="postVO"}
    							<li><a href="#" class="postLink">{$postVO->title}</a> &mdash; {$postVO->location} &mdash; <b> ${$postVO->price} </b>
    							<br /><b>Author:</b> {$postVO->author}
    							<br /><b>ISBN:</b> {$postVO->ISBN}
    							<br /><b>Created:</b> {$postVO->datePosted|date_format}
    							<br /><b>Expires:</b> {$postVO->datePosted|addDays:$smarty.const.EXPIRE_DAYS|date_format}
    							<div class="right"> <a href="#">[ Edit ]</a> - <a href="#">[ Delete ]</a></div>
    							<div class="hrzln" ></div></li>	
    						{foreachelse}
    							<li>No Book Posts, click on New Post to add a book for sale</li>
    						{/foreach}
    	
    					</ol>
    Hope it helps somebody, Suggestions welcome,





    Dan

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      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.

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        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

        Comment

        Working...