The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
[HOW TO - vB4] Rendering templates and registering variables - a short guide
Introduction Starting with vB4, templates no longer get output using eval: PHP Code:
What's more: Variables and arrays from plugins that are executed on a page no longer can automatically be accessed in the templates of that page. They need to be registered first. . Basic functionality to render templates and register all variables/arrays you want to use inside PHP Code:
HTML Code:
{vb:raw my_var} {vb:raw my_array.key1} {vb:raw my_array.key2.key21} . . . Now, with the result of the rendering we can do several things: . Output template directly - custom pages PHP Code:
Note the second line, which is special for this type of use: PHP Code:
. Use a template hook PHP Code:
. Save into a variable for later use in custom template PHP Code:
PHP Code:
HTML Code:
{vb:raw my_template_rendered} . Save into an array and preregister to use in an existing/stock template PHP Code:
HTML Code:
{vb:raw my_insertvar} Essentially the same as what I put for preRegister would be the following two lines. They could replace the last two lines in the above php codebox: PHP Code:
. . Bonus track: ...whatever you do, cache your templates! Now you know how to get your templates on screen - once you succeeded in doing that, make sure to do it in a fast and ressource saving manner: make use of vB's template cache. To see whether your templates are cached or not, activate debug mode by adding $config['Misc']['debug'] = true;to your config.php (don't ever use that on your live site!). Among the debug info is a list of all templates called, and non-cached templates will show up in red. To cache your templates, add a plugin at hook cache_templates with the following code: PHP Code:
. Hope this helps! -cel ---- Addendum - There are now two blog posts on vb.com related to this topic: http://www.vbulletin.com/forum/entry...in-4-templates http://www.vbulletin.com/forum/entry...-4-based-files |
#162
|
||||
|
||||
Quote:
|
#163
|
|||
|
|||
Hello everyone, I have been reading this article over and over again trying to figure out what I am doing wrong. Basically I am trying to put the contents of one template into another using a plug in. The template in question are:
-OFTW -COFTW_FAQ I want to be able to put the contents of template COFTW_FAQ into a variable that I can then use in OFTW template. I am using a script (oftw.php) which uses the OFTW template; I need to be able to insert the contents of COFTW_FAQ into that template using a plug-in. So this is what I have so far: Plug-in 1: Hook: Global Start PHP Code:
PHP Code:
Code:
{vb:raw COFTW_FAQ} Please Help I have been hitting myself in the head for the last 4 hours! --------------- Added [DATE]1310166367[/DATE] at [TIME]1310166367[/TIME] --------------- UPDATE!!! After more thinking... I can see that I was a bit off, I am now using only plug in to try and accomplish what I want but still just blank... Hook Location: Process Templates Complete PHP Code:
--------------- Added [DATE]1310224935[/DATE] at [TIME]1310224935[/TIME] --------------- GOT IT!!! FINALLLLY!!!!! xD I had to just register my variable in the actual oftw.php file . Since it was a custom page the script did not have the variable registered so I added PHP Code:
|
#164
|
||||
|
||||
Congrats - good to see you got it working
|
#165
|
||||
|
||||
Quote:
global_bootstrap_init_start should be used instead of global_start in most circumstances. |
#166
|
|||
|
|||
Quote:
|
#167
|
||||
|
||||
If you are going to assign several variables in a row, you may want to use the quickRegister function, as it is a lot more readable and clear what is happening (IMO).
PHP Code:
|
#168
|
||||
|
||||
Quote:
|
#169
|
||||
|
||||
As a general rule: Use a hook that is only called where you need the variable. Normally, you execute code before registering to get the values, and you want to run that only if it's needed. Of course, using stuff like if THIS_SCRIPT will do the job, too.
|
#170
|
|||
|
|||
i learned how to make custom templates like this. I have a variable that i want to be able to use on the header on every page in vbulletin. I cant get it to save my variable globaly tho. I'm only able to use this var within the template. I read the part where it talked about Save into an array and preregister to use in an existing/stock template. but could not get it to work. by the way, I have no idea how to show code on here
Code:
$eventlist = mysql_query("SELECT * FROM thread WHERE forumid = 8 ORDER BY dateline DESC", $connection);
if (!$eventlist) {
die("Database selection failedquery:SEEPASSWORD: " . mysql_error());
}
while ($row = mysql_fetch_array($eventlist)) {
$zthreadid .= $row["threadid"];
$zdateline .= $row["dateline"]."<br />";
$zlink .= "<a href='showthread.php?{$row["threadid"]}'>" . $row["title"]."</a>" . "<br />";
}
$templater = vB_Template::create('threads');
$templater->register('zthreadid', $zthreadid);
$templater->register('zlink', $zlink);
$templater->register('zdateline', $zdateline);
$zlink2['$zlink'] = $templater->render();
vB_Template::preRegister('header',array('zlink2' => $zlink2));
|
#171
|
||||
|
||||
Just paste your code you're using inside code/html/php tags. For example:
[code.]your code here[./code] Would be Code:
your code here Would be PHP Code:
Would be HTML Code:
your code here |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|