vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin Tips & Tricks (https://vborg.vbsupport.ru/forumdisplay.php?f=239)
-   -   Perform simple calculations inside templates! (https://vborg.vbsupport.ru/showthread.php?t=188930)

Lea Verou 08-22-2008 10:00 PM

Perform simple calculations inside templates!
 
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:
HTML Code:

<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):
Quote:

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 08-30-2008 12:19 AM

interesting, thanks!

Kaelon 09-24-2008 12:39 AM

Very cool! Thanks, Michelle!

Chunky Monkey 10-21-2008 10:38 PM

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 10-21-2008 10:41 PM

Nope. The functions for random number generation aren't allowed in templates, so you have to use a plugin.

Chunky Monkey 10-21-2008 11:33 PM

Thank you, Michelle.

gmert 05-26-2009 06:49 AM

Could I change the numbers by myself ????????
for examble, the number 41, to put the number 88 or number 14 ????







ragtek 05-26-2009 07:09 AM

Quote:

Originally Posted by Michelle (Post 1650030)
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 05-26-2009 07:18 AM

How could i do it ??????

L2V 07-14-2009 10:42 PM

Anyone know what kinds of simple calculations can be done using this method?

Lea Verou 07-14-2009 11:01 PM

Quote:

Originally Posted by L2V (Post 1848852)
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 08-13-2009 02:18 PM

Thanks for sharing.

Could you do a multiplication? So

$newfigure = $oldfigure + $oldfigure * 0.15

Any idea?

Lea Verou 08-13-2009 02:27 PM

Yes you can.

xlguy 08-13-2009 02:33 PM

Thanks, sorry I should have been a bit clearer, what would the syntax be? :)

Lea Verou 08-13-2009 04:58 PM

Code:

<if condition="$newfigure = $oldfigure + $oldfigure * 0.15"></if>
And after that, use $newfigure normally in your template

TimberFloorAu 09-03-2009 04:58 AM

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 09-03-2009 06:04 AM

If I understood what you're trying to do correctly, that's not possible in the templates alone.

TimberFloorAu 09-03-2009 09:50 PM

Ok thanks, back to the drawing board.

Dax IX 10-21-2009 05:36 PM

This is a great tip! I've already found a couple of uses for it. :)


All times are GMT. The time now is 01:00 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01235 seconds
  • Memory Usage 1,748KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)bbcode_html_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (19)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete