473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Addition or Subtraction in smarty

dlite922
1,584 Expert 1GB
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
Dec 8 '08 #1
3 20732
dlite922
1,584 Expert 1GB
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
Dec 8 '08 #2
Markus
6,050 Expert 4TB
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.
Dec 8 '08 #3
dlite922
1,584 Expert 1GB
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
Dec 9 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Daan | last post by:
Hello all, I'm having a little problem with Smarty. Getting it to work in a simple case went fine, but now I have the following situation: / /smarty /templates template.tpl /templates_c
1
by: Richard | last post by:
I need help. I have a smarty based program that I am modifying. I have a smarty template file that consists of smarty and HTML. I need to integrate some PHP database calls into it. My problem...
2
by: M | last post by:
Hi Folks, I am trying to install Smarty using PHP 5 on a Windows XP PC using IIS v6 The following PHP File: <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head>
0
by: Matt | last post by:
geez, i've done this a million times, you'd think i could get it right. uploading to production server for a new site, and part of the head.inc that starts everything up looks like this: echo...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
0
by: Conrad | last post by:
Hi, I'm very new to Smarty Template Engine and I'm using PHP 5.0.4 on the Windows XP Professional platform and I have the following question: When I change the 7th line of the script to use a...
5
by: Paul | last post by:
Why is the addition here adding the number as a string but the subtraction works fine? function boxchange(box) { iv= dbform.ut.value if (box.checked == true) { iv = iv - box.value } else {...
3
by: muler | last post by:
hi all, After reading this excerpt from "The C# Programming Language", (By Anders) I tried to check it out. Unfortunately, I'm getting compile errors. Can anyone illustrate this with an...
1
by: ajaymohank | last post by:
hello friends.......... i am new to smarty. can anyone please tell me how to get the value from a dynamically generated checkbox using smarty. this is the code i used... here i am generating 10...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.