PDA

View Full Version : Perform simple calculations inside templates!


Lea Verou
23 Aug 2008, 20:53
I was quite excited to find this out!

Have you ever wanted to make a simple calculation for a template, like an addition or subtraction? Personally, I needed this many times and most of them I created a plugin for that. Today I discovered that that is unecessary. You can use the conditional to make simple calculations. For instance, lets suppose you want to display somewhere the userid of the current user, incremented by one. You could do this:

<if condition="$nextid = $bbuserinfo['userid']+1"></if>
The next userid is $nextid.


This will display as (given that the userid of the user viewing is 1):
The next userid is 2.

Of course the userid example is very simple, just to make you understand what I'm talking about.

Let me know if it helped you! :)

Antivirus
30 Aug 2008, 01:19
interesting, thanks!

Kaelon
24 Sep 2008, 01:39
Very cool! Thanks, Michelle!

Chunky Monkey
21 Oct 2008, 23:38
Is there a way we can use this to generate a random number and then test against it so that something happens only 1/X times?

Lea Verou
21 Oct 2008, 23:41
Nope. The functions for random number generation aren't allowed in templates, so you have to use a plugin.

Chunky Monkey
22 Oct 2008, 00:33
Thank you, Michelle.

gmert
26 May 2009, 07:49
Could I change the numbers by myself ????????
for examble, the number 41, to put the number 88 or number 14 ????






https://www.vbulletin.org/forum/external/2009/05/43.jpg

ragtek
26 May 2009, 08:09
Nope. The functions for random number generation aren't allowed in templates, so you have to use a plugin.

Mmaybe you could put it into the $safe_functions variable and use it this way;)

gmert
26 May 2009, 08:18
How could i do it ??????

L2V
14 Jul 2009, 23:42
Anyone know what kinds of simple calculations can be done using this method?

Lea Verou
15 Jul 2009, 00:01
Anyone know what kinds of simple calculations can be done using this method?
Anything that doesn't involve functions or the functions involved are allowed within templates.

xlguy
13 Aug 2009, 15:18
Thanks for sharing.

Could you do a multiplication? So

$newfigure = $oldfigure + $oldfigure * 0.15

Any idea?

Lea Verou
13 Aug 2009, 15:27
Yes you can.

xlguy
13 Aug 2009, 15:33
Thanks, sorry I should have been a bit clearer, what would the syntax be? :)

Lea Verou
13 Aug 2009, 17:58
<if condition="$newfigure = $oldfigure + $oldfigure * 0.15"></if>And after that, use $newfigure normally in your template

TimberFloorAu
03 Sep 2009, 05:58
What would be the format for:

Displaying the cumulative count of all a users posts ?

so if they have 5 threads.

thread a has 23 views
thread b has 34 views
thread 3 has 26 views

so on and so on...

and create code to echo the total of those views a+b+c etc

Lea Verou
03 Sep 2009, 07:04
If I understood what you're trying to do correctly, that's not possible in the templates alone.

TimberFloorAu
03 Sep 2009, 22:50
Ok thanks, back to the drawing board.

Dax IX
21 Oct 2009, 18:36
This is a great tip! I've already found a couple of uses for it. :)