PDA

View Full Version : First Look at vBulletin 4 Template Variables


Wayne Luke
01 Jul 2009, 14: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: {vb:type value.key}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:

$bbuserinfo[userid] => {vb:var bbuserinfo.userid}
$bbuserinfo[musername] => {vb:raw bbuserinfo.musername}
$vbphrase[welcome] => {vb:rawphrase welcome}
$stylevar[imgdir_misc] =>{vb:stylevar imgdir_misc}
VAR
{vb:var variable}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
{vb:raw variable}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
{vb:phrase phrase, param1, param2...}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
{vb:rawphrase phrase, param1, param2...}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
{vb:stylevar variable}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.


<vb:if condition="$show['bbcodephp']">
<div class="block bbcodeblock">
<h2 class="blockhead">{vb:rawphrase php_code}<a name="php"></a></h2>
<div class="blockrow">
<h3 class="blocksubhead">{vb:rawphrase php_tag_performs}</h3>
<ul class="codeblock">
<li class="blockrow floatcontainer">
<div class="desc"><span class="highlight">{vb:rawphrase value}</span></div>
<div class="title" width="20%">{vb:rawphrase usage}</div>
</li>
<li class="blockrow floatcontainer">
<div class="desc"><br />
$myvar = 'Hello World!';<br />
for ($<i></i>i = 0; \$i &lt; 10; \$i++)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo $myvar . &quot;\n&quot;;<br />
}<br />
</div>
<div class="desc">{vb:rawphrase example_usage}</div>
</li>
<li class="blockrow floatcontainer">
<div class="desc">{vb:raw specialbbcode.php}</div>
<div class="title" width="20%">{vb:rawphrase example_output}</div>
</li>
</ul>
</div>
</div>
</vb:if>

Claiver
25 Dec 2009, 22:43
Cool, though I don't get it why we can't use normal PHP anymore, way more clarified!

TheSupportForum
17 Jan 2010, 22:06
can anyone help me with this i am unsure what to chnage it to

value="$bbuserinfo[email]"

BBR-APBT
17 Jan 2010, 22:59
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:

value="{vb:raw bbuserinfo.email}"

TheSupportForum
17 Jan 2010, 23:22
Please explain a little better what you are wanting to do.

I think you are wanting to do this:

value="{vb:raw bbuserinfo.email}"


thx for the help


do you know anything of this
$vbgpc[additional_usernames]

BBR-APBT
18 Jan 2010, 03:05
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, 23: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, 22:42
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, 11: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, 14:49
can someone help me with this code

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



if (isset($vbulletin->options['vb4_error_show_notices']) and !$vbulletin->
options['vb4_error_show_notices'] and in_array(THIS_SCRIPT, array('400_forum',
'401_forum', '403_forum', '404_forum', '500_forum')))
{
$show['notices'] = false;
}

TheSupportForum
27 Feb 2010, 16:07
can anyone help solve this problem

i want to include the following within a template


<!--#echo var="REMOTE_ADDR" -->
<!--#echo var="HTTP_USER_AGENT" -->
<!--#echo var="REQUEST_URI" -->
<!--#echo var="HTTP_HOST" -->


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, 13: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
$namelist = vB_Template::create('names_loop')->render();


And the template that should display the whole looped conteend

$templater = vB_Template::create('NAMES');
$templater->register('names', $data);
$templater->register('names_loop', $namelist);
print_output($templater->render());



$data is an array like

$data = array(0 => array('name' => 'testuser', 'age' => 100), 1 => array('name' => 'testuser 2', 'age' => 99));



My "NAMES" template looks like this ...

<div>{vb:raw names_loop}</div>



My "names_loop" template looks like this ...

<div style="magin: 10px; padding: 10px;">
<div>{vb:raw names.name}</div>
<div>{vb:raw names.age}</div>
</div>


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
18 Mar 2010, 00: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, 10:10
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



<div id="pagetitle" class="pagetitle">
<h1>Thread: <span class="threadtitle">{vb:raw msgbox}</a></span></h1>
</div>



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:



$msgbox = "Hello World";
$templater = vB_Template::create('mytemplate');
$templater->register('msgbox', $msgbox);
$template_hook[showthread_above_posts] .= $templater->render();



Plugin Is Active: Yes

Ducks
23 Mar 2010, 10:37
Thanks, it's working now

Jaxel
14 Apr 2010, 21:07
Does anyone know how to get forumIDs and bbuserinfo from this new system?

I'm trying to use it as follows, but it doesn't work...

header.php?fid={vb:var forum.forumid}&pid={vb:var bbuserinfo.field9}


I'm trying to use this in the HEADER template. It seems that you can no longer call forum and user information in the header template. So it seems forcing specific headers to specific forums is IMPOSSIBLE in VB4.

TheSupportForum
18 Apr 2010, 11:03
i have a template i wish to add to the navbar template

i called it template2

i want to place my custom template above

<div id="breadcrumb" class="breadcrumb">
<ul class="floatcontainer">

i tried
{vb:raw navbar2}

link this


{vb:raw navbar2}
<div id="breadcrumb" class="breadcrumb">
<ul class="floatcontainer">


but did not work, my template name is navbar2

Jaxel
22 Apr 2010, 07:06
How would we use $vbulletin->options in this?

I am trying to use {vb:options media_thumb_dir} but its not working...

James T Brock
25 Apr 2010, 05:20
So basically the variables were changed for no reason just so that non-coders could understand it? Those new variables look a lot more confusing to me.

Zachery
25 Apr 2010, 06:26
Not sure why it was changed, but Jaxel:

{vb:raw vboptions.media_thumb_dir}

Abhik
13 May 2010, 05:53
Can someone please convert these for me?

$spacer_open
$spacer_close

TalkVirginia
13 May 2010, 07:05
Can someone please convert these for me?

$spacer_open
$spacer_close

I haven't tested this but it may be something like:

{vb:raw spacer_open}
{vb:raw spacer_close}

unless 4.x has a different mechanism.

Double check the brackets, I'm not sure if they are correct.

Abhik
13 May 2010, 12:03
Nope.. they aren't correct. I already tried that before posting here :)

bananalive
13 May 2010, 13:14
Nope.. they aren't correct. I already tried that before posting here :)
TalkVirginia is right:
{vb:raw spacer_open}
{vb:raw spacer_close}

You might need to pre register the variable, either in plugin or php file.

vB_Template::preRegister('YOUR_TEMPLATE',array('spacer_open' => $spacer_open, 'spacer_close' => $spacer_close));

Abhik
13 May 2010, 13:59
TalkVirginia is right:
{vb:raw spacer_open}
{vb:raw spacer_close} You might need to pre register the variable, either in plugin or php file.

vB_Template::preRegister('YOUR_TEMPLATE',array('spacer_open' => $spacer_open, 'spacer_close' => $spacer_close));
Can you tell me how to do that as a plugin?

Davidinh
24 May 2010, 15:46
Hi everyone

i have a php file that contain some variable and i tried to connect them together like a URL (link)
$folder = $folders = $vbulletin->options['folder'];
$subfolder = $folders = $vbulletin->options['subfolder'];
$file = $folders = $vbulletin->options['file'];

$url = $folder/$subfolder/$file;
then i got an error like

Warning: Division by zero in E:\xampp\htdocs\test\test.php on line 184

note: those code on line 184

any help or suggestion will be appreciated

or with those variable how can i put them in my template
{vb:var folder}/{vb:var subfolder}/{vb:var file}
within $templater->register('');

is it correct ?

thanks,

TalkVirginia
24 May 2010, 17:28
Hi everyone

i have a php file that contain some variable and i tried to connect them together like a URL (link)
$folder = $folders = $vbulletin->options['folder'];
$subfolder = $folders = $vbulletin->options['subfolder'];
$file = $folders = $vbulletin->options['file'];

$url = $folder/$subfolder/$file;
then i got an error like

Warning: Division by zero in E:\xampp\htdocs\test\test.php on line 184

note: those code on line 184

any help or suggestion will be appreciated

or with those variable how can i put them in my template
{vb:var folder}/{vb:var subfolder}/{vb:var file}
within $templater->register('');

is it correct ?

thanks,

If you are going to use your first example you need to enclose your slashes in quotes like so... $url = $folder . '/' . $subfolder . '/' . $file however I'm sure there is a better way of doing this. Maybe reading the folder path and parsing each part out into separate variables depending on what you want to do.

Davidinh
25 May 2010, 00:12
thank you very much TalkVirginia
it works

Frank T
22 Jun 2010, 04:44
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



<div id="pagetitle" class="pagetitle">
<h1>Thread: <span class="threadtitle">{vb:raw msgbox}</a></span></h1>
</div>



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:



$msgbox = "Hello World";
$templater = vB_Template::create('mytemplate');
$templater->register('msgbox', $msgbox);
$template_hook[showthread_above_posts] .= $templater->render();



Plugin Is Active: Yes

What's the difference between the "template hook" and "hook location?" Why aren't they the same thing? You've used "showthread_above_posts" template hook and "showthread_complete" hook.

noppid
22 Jun 2010, 16:42
The template hook, $template_hook['showthread_above_posts'] for example, is the variable you want to populate to display your data. The hook location, showthread_complete for example, is the hook in the php file that you use to load the template variable with your data by rendering a template for instance. You can select these hook locations in the plugin manager drop down when you create a new plugin.

Frank T
22 Jun 2010, 19:39
The template hook, $template_hook['showthread_above_posts'] for example, is the variable you want to populate to display your data. The hook location, showthread_complete for example, is the hook in the php file that you use to load the template variable with your data by rendering a template for instance. You can select these hook locations in the plugin manager drop down when you create a new plugin.

OK, that helps a lot. Now, next question. What if I want to render into my own private variable and display as part of the "postbit_legacy" template? For example, if I wanted to render as follows:

$MyVar = $templater->render();

...

I registered my PHP code at postbit_display_start.

Then inside of "postbit_legacy" -- replace {vb:raw post.message} with {vb:raw MyVar}. There appears to be a variable scope problem, or I'm not accessing $MyVar correctly inside of postbit_legacy.

When I render into one of the hooks, the rendered display does show up correctly -- so I know the mechanics of what I'm doing are all correct.

BirdOPrey5
04 Jul 2010, 03:17
So basically the variables were changed for no reason just so that non-coders could understand it? Those new variables look a lot more confusing to me.

I agree and this definitely helps cement by decision to never upgrade to vb4.

Clamshells
03 Sep 2010, 20:18
Does anyone know the usergroup HTML markup variable names?

To be clear, I'm not looking to use {vb:raw post.musername}, I want to get the HTML markup without the username.