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 |
#152
|
||||
|
||||
Quote:
Code:
$myvar = $templater->render(); should be this: Code:
$myvar .= $templater->render(); |
#153
|
||||
|
||||
I'm struggling with getting data from a variable into one of the stock templates. Here's what I've done:
Created a custom plugin with the hook location set to "global_start": $roscohtml = "<div>HELLO WORLD!</div>"; error_log($roscohtml); vB_Template:reRegister('header',array('roscohtml '=>$roscohtml)); Then I customized the header template to contain this: <div class="above_body"> <!-- closing tag is in template navbar --> <div>test before</div> {vb:raw roscohtml} <div>test after</div> Unfortunately the variable isn't being output. I do see the contents in the error log, so I know it's firing. Any tips? Thanks, Rosco --------------- Added [DATE]1302039172[/DATE] at [TIME]1302039172[/TIME] --------------- OK, I think I found the problem - the global_start hook is not the right place for this to live. I moved my code to the parse_templates hook and the data is now available. Is there a document which outlines the best way to leverage the hooks, or at least where each is called in the flow? Rosco |
#154
|
|||
|
|||
as templates no longer get output using eval
i tried to change it but seems not working. can anyone help me with this code? PHP Code:
|
#155
|
||||
|
||||
<a href="https://vborg.vbsupport.ru/showpost.php?p=2180014&postcount=151" target="_blank">https://vborg.vbsupport.ru/showp...&postcount=151</a>
|
#156
|
||||
|
||||
I hoping this isn't too far off topic bt if possible I think it would make a good addition to the article...
What if I want to override an existing template with a new template. Basically ignore whatever is in a given template and render a new template, and copy the new template to the old template? Is that possible? I've tried stuff like: Code:
$templater = vB_Template::create('my_new_template'); $templater->register('myvar', $myvar); $globaltemplates['existing_vb_template'] = $templater->render(); Is what I'm asking possible? Seems like it should be... Any input would be appreciated. |
#157
|
||||
|
||||
Yes, it can be done easily, if what you are wanting is what I understand it to be. As an example, here is what I did for that:
Code:
$templater = vB_Template::create('boofo_forumhome_event'); $templater->register('callink', $callink); $templater->register('daysevents', $daysevents); $templater->register('eventdate', $eventdate); $templater->register('eventdates', $eventdates); $upcomingevents .= $templater->render(); $vbulletin->templatecache['forumhome_event'] = $vbulletin->templatecache['boofo_forumhome_event']; |
#158
|
||||
|
||||
Great, Thanks Boofo!
It might be worth mentioning for anyone else- you have to register any variables used to the ORIGINAL template (the one you are overriding) not the new template... At least that's the only way it's working for me. (On parse_templates hook) |
#159
|
||||
|
||||
Are you sure? I used the same variables when I did it but I would think you only need to pre-register the variables you are going to use, not the other way around.
I did the above code in forumhome_start. Did you try the process_templates_complete hook? |
#160
|
||||
|
||||
Maybe I didn't explain it right... in your example registering callink and the others to boofo_forumhome_event didn't do anything for me- I had to preRegister them to forumhome_event for them to work. I tried both parse_templates and process_templates_complete hooks.
To me it makes sense since the forum thinks it's outputting forumhome_event, and this is confirmed by viewing template names in the HTML source code. |
#161
|
||||
|
||||
Well, yes and no. Since I did it in the forumhome_start hook, it needed those pre-registered for my new template. As long as the variables are good at that hook, then it will work the way I did it.
The forumhome doesn't care about the old template as long as I pre-register everything for my new template. Doing it in the parse_templates hook might be why it didn't work for you with your code. You would have to check whether whatever variables you are pre-registering have already been validated at whatever hook you are using. Now I'm getting confused. And it hurts! LOL |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|