vBulletin Mods

The Official vBulletin Modifications Site
https://www.vbulletin.org/forum/showthread.php?t=217569

First Look at vBulletin 4 Template Variables
by Wayne Luke
01 Jul 2009 13:09

Reposted from my site at www.vbcodex.com

Actually, Scott's blog was the first look but I am going to talk about vBulletin 4's Template Variables today.

Back in the beginning, it was decided that vBulletin needed an easy to use template system. This also required that we allow PHP variables that could be evaluated and displayed to the end user in order to get the data out. This was simple and easy since PHP at its beginning was just a simple templating engine itself. However over time, it became apparent that we needed our own structures in order to coax HTML along and present dynamic data to the user while allowing the designer/site owner control and not requiring knowledge of PHP. With that the <if> and <phrase> tag constructs were developed in 3.0 and there was much rejoicing.

Later it became apparent that programmers wanted to be able to easily inject their own code into the templates without bothering the site owner. At first they used regular expressions and other replacement techniques. However as templates were updated or changed, they could break these techniques so the concept of Template Hooks were born. Now developers could assign their code to a specific variable and have it appear without the site owner doing anything. Unfortunately, template hooks aren't used as widely as they could be so the above techniques are still used.

The problem though with template hooks, <if> conditionals and <phrase> tags is that it made it very difficult to work with templates in tools like Dreamweaver and Go Live!. Syntax highlighting was off, variables didn't render and preview was a general mess. With the move to an object oriented template system in vBulletin 4, this would have become even worse over time. So the developers decided to move vBulletin's proprietary tags to their own namespace and create a better way of presenting variables within the templates.

For variables, they adopted a 'curly node' syntax that moves away from the traditional PHP variable system and tells the system how to process each type of variable. vBulletin 4 will ship with support for a variety of these variables for different purposes. The variable name tells vBulletin how to process the output. Today, I want to go over some of these.

Variable format:
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This format tells vBulletin that it needs to process the variable through curly node syntax. The type tells vBulletin which class to use. If you look at the format above, you'll see there are no $ or [] used. This is an attempt to make the variable easier to use for non-programmers. So instead we'll use a 'dot' notation for variables. As a designer and developer, you'll use several types primarily. These are var, raw, phrase, rawphrase, stylevar and link. I'll talk about the first five today.

Examples:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

VAR

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Allows you to output any variable. The output will be processed through htmlspecialchars_uni() to prevent any kind of potential XSS issues.

If your variable outputs HTML code, then you'll want to use raw instead.

RAW

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This will bypass encoding the output to make it safe. This should be used to information that is considered safe and verified in PHP already. An example would be including another template within the current template like the header template. Raw should never be used with user input.

PHRASE

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Allows you to process and output phrases that are available to the page. The system will include as many parameters as listed in the parsing of the phrase. The phrase will be passed through htmlspecialchars_uni() to make it safe.

If the phrase includes HTML code, you should use rawphrase instead.

RAWPHRASE

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Same as phrase above but does not encode the output to make it safe. As with raw variables, this should only be used for data that is verified as safe within the PHP code.

STYLEVAR

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This is a shortcode. You could use {vb:var stylvar.variable} as well. However this allows you to differentiate between stylevars and standard variables. It ultimately makes the code easier to read and should be used for any style variables that you want to use.

Summary
These are just some of the variables being used. There are other types that I will cover later. These include the previously mentioned link but also include date, time, number, if, escapejs, urlencode, and math. To finish today, I will leave you with a template example.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


Claiver 25 Dec 2009 21:43

Cool, though I don't get it why we can't use normal PHP anymore, way more clarified!

TheSupportForum 17 Jan 2010 21:06

can anyone help me with this i am unsure what to chnage it to

value="$bbuserinfo[email]"

BBR-APBT 17 Jan 2010 21:59

Quote:

Originally Posted by simonhind (Post 1958565)
can anyone help me with this i am unsure what to chnage it to

value="$bbuserinfo[email]"

Please explain a little better what you are wanting to do.

I think you are wanting to do this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


TheSupportForum 17 Jan 2010 22:22

Quote:

Originally Posted by BBR-APBT (Post 1958629)
Please explain a little better what you are wanting to do.

I think you are wanting to do this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

thx for the help


do you know anything of this
$vbgpc[additional_usernames]

BBR-APBT 18 Jan 2010 02:05

Quote:

Originally Posted by simonhind (Post 1958652)
thx for the help


do you know anything of this
$vbgpc[additional_usernames]

That looks like come from a modification. Ask in that thread please.

TheSupportForum 02 Feb 2010 22:56

can anyone help me convert his old template variable to vb4 please

$vbphrase[threads]: $totalthreads
$vbphrase[posts]: $totalposts
$vbphrase[members]: $numbermembers

bananalive 05 Feb 2010 21:42

Quote:

Originally Posted by simonhind (Post 1973430)
can anyone help me convert his old template variable to vb4 please

$vbphrase[threads]: $totalthreads
$vbphrase[posts]: $totalposts
$vbphrase[members]: $numbermembers


{vb:phrase threads}: {vb:var totalthreads}
{vb:phrase posts}: {vb:var totalposts}
{vb:phrase members}: {vb:var numbermembers}

chris1979 18 Feb 2010 10:09

What is the variable for the number of unread private messages?

I used to use $vbphrase[unread_x_nav_compiled]

What's the vb4 version?

TheSupportForum 20 Feb 2010 13:49

can someone help me with this code

i've gone wrong somewhere in the code and can't figure it out


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


TheSupportForum 27 Feb 2010 15:07

can anyone help solve this problem

i want to include the following within a template


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

how can i do this and do i ned to include anything in the php file for it to work within a template

Ronny 05 Mar 2010 12:36

I can't convert my custom pages until i know how to access and loop with templates again. It is nice to know how to access one single var or single var in an array but how to loop? I did try my luck with ... (but without any success, yet ...)

Template i need to loop for output

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


And the template that should display the whole looped conteend

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


$data is an array like

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


My "NAMES" template looks like this ...

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


My "names_loop" template looks like this ...

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

So far i can access all the vars in my array with {vb:var names.0.name} or {vb:var names.0.age} but i can't use it in templates to loop an output.

Can anyone help, please? :confused:

Thanks.

Ducks 17 Mar 2010 23:35

I'm trying to use a variable in SHOWTHREAD, but it's not showing for some reason.

Hook Location: showthread_start
variable: $msgbox
template variable: {vb:raw msgbox}


What can be wrong?

TalkVirginia 19 Mar 2010 09:10

Quote:

Originally Posted by Ducks (Post 2005771)
I'm trying to use a variable in SHOWTHREAD, but it's not showing for some reason.

Hook Location: showthread_start
variable: $msgbox
template variable: {vb:raw msgbox}


What can be wrong?

Depending on where you want to place it. For example, if you want to place above the thread, use the showthread_above_posts template hook, then create a plugin using the showthread complete hook location. If you open showthread.php, examine around line 2176 and below.

Try this example:

1. Add a new custom template with the following contents:

Product: vBulletin (or if you are creating a mod, choose your product id)
Style: Style of your choice
Title: mytemplate


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

2. Create a new plugin:
Product: vBulletin (or if you are creating a mod, choose your product id)
Hook Location: showthread_complete
Execution Order: Leave as default
Plugin PHP Code:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Plugin Is Active: Yes

Ducks 23 Mar 2010 09:37

Thanks, it's working now


All times are GMT. The time now is 01:20.

Powered by vBulletin® Version 3.8.12
Copyright © 2019, MH Sub I, LLC dba vBulletin. All Rights Reserved. vBulletin® is a registered trademark of MH Sub I, LLC
Copyright ©2001 - , vbulletin.org. All rights reserved.