XF 2.3 "Math" in templates?

Wildcat Media

Well-known member
Licensed customer
I searched but couldn't find anything relevant.

Is there a way to do math in templates?

For instance, I want to use some of our stats in a template. Rather than say, "We have 93,254 members," I'd rather say something like, "We have over 90,000 members." So, a rounding down of an existing number.

Or in another instance, if we wanted to show elapsed time in a template, I would want something like "The forum was founded X years ago."

The abbreviations look kind of sloppy grammatically, so "We have over 90k members" just doesn't look right.

If this isn't possible, I'll abandon the idea. It's not worth writing an add-on for, in other words.
 
Solution
You can use floor() to round down. {$xf.app.forumStatistics.users} pulls user statistics.

Here is an example for your case if your forum has over 90,000 members.

HTML:
<p>We have over {{ number(floor($xf.app.forumStatistics.users / 10000) * 10000) }} members.</p>

You could also do...

HTML:
<xf:set var="$users" value="{$xf.app.forumStatistics.users}" />

<p>We have over {{ number(floor($users / 10000) * 10000) }} members.</p>

For the other one, I don't think XenForo records the date of first install, but you can use a UNIX timestamp for the date.

HTML:
<xf:set var="$founded" value="1451606400" /> // January 1, 2016

<p>The forum was founded {{ floor(($xf.time - $founded) / 31536000) }} years ago.</p>

This would come...
You can use floor() to round down. {$xf.app.forumStatistics.users} pulls user statistics.

Here is an example for your case if your forum has over 90,000 members.

HTML:
<p>We have over {{ number(floor($xf.app.forumStatistics.users / 10000) * 10000) }} members.</p>

You could also do...

HTML:
<xf:set var="$users" value="{$xf.app.forumStatistics.users}" />

<p>We have over {{ number(floor($users / 10000) * 10000) }} members.</p>

For the other one, I don't think XenForo records the date of first install, but you can use a UNIX timestamp for the date.

HTML:
<xf:set var="$founded" value="1451606400" /> // January 1, 2016

<p>The forum was founded {{ floor(($xf.time - $founded) / 31536000) }} years ago.</p>

This would come out to over 10 years ago.
 
Last edited:
Solution
You can use floor() to round down. {$xf.app.forumStatistics.users} pulls user statistics.

Here is an example for your case if your forum has over 90,000 members.

HTML:
<p>We have over {{ number(floor($xf.app.forumStatistics.users / 100000) * 10000) }} members.</p>

You could also do...

HTML:
<xf:set var="$users" value="{$xf.app.forumStatistics.users}" />

<p>We have over {{ number(floor($users / 10000) * 10000) }} members.</p>

For the other one, I don't think XenForo records the date of first install, but you can use a UNIX timestamp for the date.

HTML:
<xf:set var="$founded" value="1451606400" /> // January 1, 2016

<p>The forum was founded {{ floor(($xf.time - $founded) / 31536000) }} years ago.</p>

This would come out to over 10 years ago.
Genius! Thanks much @Matt C. ! 👍👍

I am able to pull forum stats but only the raw numbers. This will clean everything up nicely.

And yes, I assumed I was going to need to manually enter a date (timestamp) for when the forum was founded, so that also works out perfectly. (We started on vBulletin in 2002.)
 
Back
Top Bottom