Back to vBulletin 4 Articles

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

Reposted from my site at http://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:
Code:
{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:
Code:
$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
Code:
{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
Code:
{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
Code:
{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
Code:
{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
Code:
{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.

HTML Code:
<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">[php]<span class="highlight">{vb:rawphrase value}</span>[/php]</div>
                    <div class="title" width="20%">{vb:rawphrase usage}</div>
                </li>
                <li class="blockrow floatcontainer">
                    <div class="desc">[php]<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 />
                    [/php]</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>

vblts.ru supports vBulletin®, 2022-2024