Page 1 of 1

Math in a news summary template

Posted: Wed Oct 04, 2017 7:49 am
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"}

SOLVED: Math in a news summary template

Posted: Sun Oct 08, 2017 5:51 pm
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"}

Re: Math in a news summary template

Posted: Mon Oct 09, 2017 8:58 am
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'}

Re: Math in a news summary template

Posted: Tue Oct 17, 2017 8:32 pm
by cpansewicz
Thank you so much showing me that!

Re: Math in a news summary template

Posted: Wed Oct 18, 2017 3:36 pm
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

Re: Math in a news summary template

Posted: Wed Oct 18, 2017 4:37 pm
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!