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 |
#42
|
||||
|
||||
Quote:
Good morning, er, evening cellarius! |
#43
|
||||
|
||||
Quote:
Quote:
|
#44
|
|||
|
|||
Please i need a little of help, i can't get this working..!!
I created a template called: contenedor_de_foro So i only write "Hi" inside it. I want to get "Hi" in my Header template so i made a plugin on global_start and put: Code:
$templater = vB_Template::create('contenedor_de_foro'); $templater->register_page_templates(); $contenedor = $templater->render(); vB_Template::preRegister('header', $contenedor); |
#45
|
|||
|
|||
Do we need to register stylevars as well?
|
#46
|
|||
|
|||
Hi cellarius,
Thanks for the great article I running into a bit of a problem, I can see the solution right in front of me but for some reason can't quite get it. Bassically I'm making a plugin so I have the breadcrumbs in the header. This is what I've got so far setup on the parse_templates hook: Code:
$templater = vB_Template::create('breadcrumbs'); vB_Template::preRegister( 'breadcrumbs', array('navbits'=> $navbits) ); $templatevalues['breadcrumbs'] = $templater->render(); vB_Template::preRegister('header', $templatevalues); Thanks |
#47
|
|||
|
|||
Thank you cellarius for this!
I've even printed this out and read this even while sitting on the throne quite a few times. I got some of the options working for a custom about us page. The option to - Output template directly - custom pages works just great for VB4. The option to - Save into a variable for later use in custom template works only after noticing an error in this code. In VB4 This resulted in a blank white page. PHP Code:
PHP Code:
Then option to - Save into an array and preregister to use in an existing/stock template. Unfortunately I was unable to get this working. I could not locate the template hook "forumhome_wgo_pos2" in VB4. As for Caching Templates, I didn't see any templates in red on because it this doesn't work on server that is ran locally. I've managed to get this working on an actual server. EDIT: I does work locally in my testing environment, I added... PHP Code:
Thanks for this write up, It helped me quite a bit in understanding more and more about VB. |
#48
|
||||
|
||||
Quote:
Quote:
Quote:
|
#49
|
|||
|
|||
It was a lot better than reading video game manuals.
Quote:
At first I was using your example to familiarize myself on how things work. PHP Code:
did not match second argument in the register function. $templater->register('my_template_rendered', $my_template_rendered); So I changed this: $templater->register('my_template_rendered', $my_template_rendered); to: $templater->register('my_template_rendered', $mytemplate_rendered); And everything worked fine. Quote:
My guess is that I'm not fully understanding this particular option. I've tried this on a clean install and was unable to locate this hook or maybe I'm just looking in the wrong place for it. Additional guidance will very much be appreciated if you don't mind? As for your last statement, I didn't not combine all of these options into one. I created separate custom pages for each option just to see the results of each option listed. The only one I had difficult understanding was the template hook option. |
#50
|
|||
|
|||
Alright. I can get it to display just straight templates, that used to use just eval('print_output')...........
How would I go about displaying this: Code:
eval('$userslist .= ", ' . fetch_template('userslist_bit') . '";'); |
#51
|
||||
|
||||
Quote:
Code:
$newTemplate = vB_Template::create('userslist_bit'); $newTemplate->register('variable1', $variable1); $newTemplate->register('variable2', $variable2); $userslist .= $newTemplate->render(); |
Thread Tools | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|