Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
First Look at vBulletin 4 Template Variables
Wayne Luke
Join Date: Jan 2002
Posts: 1,690

Southern California
by Wayne Luke Wayne Luke is offline 01 Jul 2009

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.

Views: 24913
Reply With Quote
Comments
  #2  
Old 25 Dec 2009, 21:43
Claiver Claiver is offline
 
Join Date: Dec 2009
Cool, though I don't get it why we can't use normal PHP anymore, way more clarified!
Reply With Quote
  #3  
Old 17 Jan 2010, 21:06
TheSupportForum TheSupportForum is offline
 
Join Date: Jan 2007
can anyone help me with this i am unsure what to chnage it to

value="$bbuserinfo[email]"
__________________
http://www.multihunters.co.uk - all your coding needs
Reply With Quote
  #4  
Old 17 Jan 2010, 21:59
BBR-APBT's Avatar
BBR-APBT BBR-APBT is offline
 
Join Date: Feb 2009
Originally Posted by simonhind View Post
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.

Reply With Quote
  #5  
Old 17 Jan 2010, 22:22
TheSupportForum TheSupportForum is offline
 
Join Date: Jan 2007
Originally Posted by BBR-APBT View Post
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]
__________________
http://www.multihunters.co.uk - all your coding needs
Reply With Quote
  #6  
Old 18 Jan 2010, 02:05
BBR-APBT's Avatar
BBR-APBT BBR-APBT is offline
 
Join Date: Feb 2009
Originally Posted by simonhind View Post
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.
Reply With Quote
  #7  
Old 02 Feb 2010, 22:56
TheSupportForum TheSupportForum is offline
 
Join Date: Jan 2007
can anyone help me convert his old template variable to vb4 please

$vbphrase[threads]: $totalthreads
$vbphrase[posts]: $totalposts
$vbphrase[members]: $numbermembers
__________________
http://www.multihunters.co.uk - all your coding needs
Reply With Quote
  #8  
Old 05 Feb 2010, 21:42
bananalive bananalive is offline
 
Join Date: Oct 2007
Real name: Dom
Originally Posted by simonhind View Post
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}
__________________
Reply With Quote
  #9  
Old 18 Feb 2010, 10:09
chris1979 chris1979 is offline
 
Join Date: Oct 2006
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?
Reply With Quote
  #10  
Old 20 Feb 2010, 13:49
TheSupportForum TheSupportForum is offline
 
Join Date: Jan 2007
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.

__________________
http://www.multihunters.co.uk - all your coding needs
Reply With Quote
  #11  
Old 27 Feb 2010, 15:07
TheSupportForum TheSupportForum is offline
 
Join Date: Jan 2007
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
__________________
http://www.multihunters.co.uk - all your coding needs
Reply With Quote
  #12  
Old 05 Mar 2010, 12:36
Ronny Ronny is offline
 
Join Date: Jan 2002
Question

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?

Thanks.
__________________
ohhm
Reply With Quote
  #13  
Old 17 Mar 2010, 23:35
Ducks Ducks is offline
 
Join Date: Apr 2006
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?
Reply With Quote
  #14  
Old 19 Mar 2010, 09:10
TalkVirginia's Avatar
TalkVirginia TalkVirginia is offline
 
Join Date: Oct 2008
Real name: Jim
Originally Posted by Ducks View Post
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
__________________
Jim - Webadmin 3:33am Paranormal Research - History will always repeat itself but sometimes the past never leave
vb4.x Email Reminders for Inactive Users, Add Mass Child Forums for vb3.8.6 & vb4.1.2

Last edited by TalkVirginia; 19 Mar 2010 at 09:18.
Reply With Quote
  #15  
Old 23 Mar 2010, 09:37
Ducks Ducks is offline
 
Join Date: Apr 2006
Thanks, it's working now
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 00:12.

Layout Options | Width: Wide Color: