Math in a news summary template

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

Math in a news summary template

Post by cpansewicz »

Hi, I'm not sure if I can do this in my news summary template. I want to use the value in a field in a math equation. Is this the correct syntax for capturing the value in the row? Thank you!

{math equation="x - y" x=$smarty.now y=$entry->fields.row1->value assign=years} {$years|date_format:"%Y"}
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

SOLVED: Math in a news summary template

Post by cpansewicz »

I did figure this one out. In the summary template I added:

{assign var="date" value=$smarty.now|date_format:'%Y'}
{capture assign="year"}{$entry->fields.row3->value}{/capture}
{assign var="startyear" value=$date - $year}
{assign var="totalyear" value=$date - $startyear}

then in the code:
<b>Total Years:</b> {$totalyear}<br />
<b>Start Year:</b> {$startyear|string_format:"%d"}
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: Math in a news summary template

Post by Rolf »

A bit shorter:

Code: Select all

{$date = $smarty.now|date_format:'%Y'}
{$year = $entry->fields.row3->value}

{$startyear = $date - $year}
{$totalyear = $date - $startyear}

Total Years:  {$totalyear}
Start Year:    {$startyear|string_format:'%d'}
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

Re: Math in a news summary template

Post by cpansewicz »

Thank you so much showing me that!
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3492
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Math in a news summary template

Post by velden »

Though seems your original approach was close:

Code: Select all

{math equation="x - y" x=$smarty.now|date_format:"%Y" y=$entry->fields.row3->value assign=startyear}

Total Years:  {$entry->fields.row3->value}
Start Year:    {$startyear}
This looks funny to me:

Code: Select all

{assign var="startyear" value=$date - $year}
{assign var="totalyear" value=$date - $startyear}
Perhaps I'm missing some details but to me it's the same as:

Code: Select all

10 - 3 = 7
10 - 7 = 3
It seems that $totalyear == $year == $entry->fields.row3->value
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

Re: Math in a news summary template

Post by cpansewicz »

I set up our staff directory in the News Module because it could be templated easily, and staff members could update the listings themselves.

Originally, when I populated the content, the template read "Years worked here:" with the numeric value. In proofing, I pointed out that this would need to be updated every year, and somebody would need to remember to do that. I wanted to add math so that the values would auto update.

Then it was change to: "At this office since:" with the year. I didn't want to go in and update each persons entry, so again, I wanted to use math to calculate this change.

In either case, I simplified my example for when I posted the solution I came up with. It is really cool how math can be applied to templates for these situations.

Thanks again!
Post Reply

Return to “Modules/Add-Ons”