Page 1 of 1

Adding PHP Variables Listed In Smarty Template Page

Posted: Sat Jun 14, 2008 6:02 pm
by jbh
Hey,

I'm trying to get 2 values that I have listed in a smarty template from Amember membership software to add up.

For instance, in the smarty template page, this line of code:

{$d.sales|string_format:"%d"}

= # of sales. This comes from a db query I create. Ok, so to the user let's say that value is '5' when they look at the page

And the next line is:

{$d.uniq|string_format:"%d"}

= # of unique VIEWS.

All I want to do is a legal math function (division) within the smarty template, such as

$div_ratio = {$d.sales|string_format:"%d"} / {$d.uniq|string_format:"%d"}
print $div_ratio;

So, within the smarty template would be a variable that divides these 2 lines of code to create a dividend for the user to view.
But when I try to turn them into variables within the smarty template, it breaks a million times.

Is it POSSIBLE to add/subtract/divide values WITHIN the smarty template html page or must EVERYTHING be done within the php pages that display them?

Thanks

Re: Adding PHP Variables Listed In Smarty Template Page

Posted: Sat Jun 14, 2008 11:33 pm
by lonelywolf
Maybe there are some solutions for your problem.
-First(not good, it's a little complex), you can print smarty variables in seperate div tags(using id attribute to identificate). After that, you use javascript to grab those variables in div tags(maybe using document.getElement...), parse to correct types, calculating and printing results in some predefined div tags.

-Second, use {php}{/php} block in smarty - not recommend.

Re: Adding PHP Variables Listed In Smarty Template Page

Posted: Sun Jun 15, 2008 12:23 pm
by WebbieDave
Absolutely it's possible to do math within smarty templates. Make sure you're dividing numbers. Try:

{$d.sales/$d.uniq|string_format:"%d"}

Check out the smarty docs for more info.

Re: Adding PHP Variables Listed In Smarty Template Page

Posted: Sun Jun 15, 2008 2:42 pm
by jbh
Ok, I'll go there for the official tutorials. It's things like math functions and declaring vars or re-formatting strings that
seem to confuse me.

Thanks