Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
First Look at vBulletin 4 Template Variables
Wayne Luke's Avatar
Wayne Luke
Join Date: Jan 2002
Posts: 1,694

 

Southern California
Show Printable Version Email this Page Subscription
Wayne Luke Wayne Luke is offline 06-30-2009, 10:00 PM

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:
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>
Reply With Quote
  #22  
Old 05-13-2010, 03:53 AM
Abhik's Avatar
Abhik Abhik is offline
 
Join Date: Jun 2006
Location: Kolkata, India
Posts: 144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Can someone please convert these for me?

Code:
$spacer_open
$spacer_close
Reply With Quote
  #23  
Old 05-13-2010, 05:05 AM
TalkVirginia's Avatar
TalkVirginia TalkVirginia is offline
 
Join Date: Oct 2008
Location: Virginia
Posts: 545
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Abhik View Post
Can someone please convert these for me?

Code:
$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.
Reply With Quote
  #24  
Old 05-13-2010, 10:03 AM
Abhik's Avatar
Abhik Abhik is offline
 
Join Date: Jun 2006
Location: Kolkata, India
Posts: 144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Nope.. they aren't correct. I already tried that before posting here
Reply With Quote
  #25  
Old 05-13-2010, 11:14 AM
bananalive bananalive is offline
 
Join Date: Oct 2007
Location: UK
Posts: 2,802
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Abhik View Post
Nope.. they aren't correct. I already tried that before posting here
TalkVirginia is right:
HTML Code:
{vb:raw spacer_open}
{vb:raw spacer_close}
You might need to pre register the variable, either in plugin or php file.

PHP Code:
vB_Template::preRegister('YOUR_TEMPLATE',array('spacer_open' => $spacer_open'spacer_close' => $spacer_close)); 
Reply With Quote
  #26  
Old 05-13-2010, 11:59 AM
Abhik's Avatar
Abhik Abhik is offline
 
Join Date: Jun 2006
Location: Kolkata, India
Posts: 144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by bananalive View Post
TalkVirginia is right:
HTML Code:
{vb:raw spacer_open}
{vb:raw spacer_close}
You might need to pre register the variable, either in plugin or php file.

PHP Code:
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?
Reply With Quote
  #27  
Old 05-24-2010, 01:46 PM
Davidinh Davidinh is offline
 
Join Date: Apr 2005
Posts: 47
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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,
Reply With Quote
  #28  
Old 05-24-2010, 03:28 PM
TalkVirginia's Avatar
TalkVirginia TalkVirginia is offline
 
Join Date: Oct 2008
Location: Virginia
Posts: 545
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Davidinh View Post
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.
Reply With Quote
  #29  
Old 05-24-2010, 10:12 PM
Davidinh Davidinh is offline
 
Join Date: Apr 2005
Posts: 47
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thank you very much TalkVirginia
it works
Reply With Quote
  #30  
Old 06-22-2010, 02:44 AM
Frank T Frank T is offline
 
Join Date: Jun 2010
Posts: 18
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by TalkVirginia View Post
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

HTML Code:
<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:

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.
Reply With Quote
  #31  
Old 06-22-2010, 02:42 PM
noppid noppid is offline
 
Join Date: Mar 2003
Location: Florida
Posts: 1,875
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Reply


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

Forum Jump


All times are GMT. The time now is 10:05 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04676 seconds
  • Memory Usage 2,347KB
  • Queries Executed 25 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (9)bbcode_code
  • (4)bbcode_html
  • (3)bbcode_php
  • (5)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (3)pagenav_pagelink
  • (11)post_thanks_box
  • (1)post_thanks_box_bit
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete