Smarty Template & Math
Well, today I actually learned something in web design for the first time in years. That’s amazing to me, because it has felt like web technology hasn’t progressed a great deal or perhaps I just haven’t had the opportunity to do anything complex in a long time. I find if hard to get motivated to work on my own websites.
I guess my passion has been online video since my kids were born. I have learned a lot about Final Cut Express, Final Cut Studio, Flash Video, Quicktime formats and more! I must say I still love my Mac Pro. I guess I am fully converted to the Mac from Windows. I find so many things are just easier and stabler in OS X Leopard.
My wife and I were comparing identical workflows today. She was working in Windows XP. She absolutely hates Vista! Anyway we learned that working with websites in Safari on a Mac is far faster than Internet Explorer.
Anyway, I digress. My purpose in blogging tonight was to share what I learned today. I am still rarely new to the Smarty template emgine / language, though I did get some experience working with it last year when I designed an e-commerce site for a customer of mine that sells party supplies. We utilized x-cart for his website, and I just customized some pre-existing templates.
Today, I wanted to achievesomething involving math functions. You click on a category and it dynamically pulls the sub-category list for that parent category. It creates a table and sets a variable to 1. Then it displays the image and sub-category name in a table cell. It uses the {foreach} function for each sub-category and increments the variable by 1 for each cell. Once it has created 5 cells, it then closes that line and creates a new table row. I was having problems figuring out how to increment the variable. When I finally figured it out, it was obvious. The wrong code was:
{assign var= “col” value=$col+}
This actually kept adding the string “+1″ to the end of the variable! I’d end up with an output of “1+1+1+1+1″ and longer! It began to get frustrating. However, I knew it was just a syntax problem. The correct code is:
{math assign “col” equation = $col+1}
That was simple enough, and It was the breakthrough that I needed! I’m getting quite close to finishing this project, and I am happy to be learning and facing challenges again!
Well, it’s time to bring this never-ending blog to a close. I do believe this is the most I have ever typed on my iPhone at any given time – ever! My arms and fingers are actually hurting now. And yes, 100% of this blog was written in bed, lights out, next to my wife… On an iPhone. /bow


Prakash said,
August 27, 2008 at 7:35 am
Thanks for the information on how to do math and assign it to another variable.
I in fact had to add two values and assign to another variable.
So I tried this method:
{math assign “myTotal” equation = $myTotal+$otherVar}
I however got similar string concatenation, so the actual code that worked for me is as follows.
{math assign=”myTotal”” equation=”X + Y” X=$myTotal Y=$otherVar }
Prakash said,
August 27, 2008 at 7:36 am
Thanks again
Prophet said,
October 15, 2008 at 8:29 pm
Hey Joel did you play Rainbow 6 back in the day? Or is it mistaken identity, my old computer got a bug, swapped the drive, but lost some contacts. Reasonable to assume you are you. John from Alaska? How have you been man? Been a while.