![]() |
[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} . . . 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 |
Great article, cellarius! Thanks for taking the time to write this out.
|
I saw the same questoins over and over again, and you and others heroically answering the same time and again, so I thought this might come in handy. That's the system as I managed to grasp it up to now, and I myself learned while typing it down. However, I'd happily add any suggestions and improvements :)
|
Thanks for the write up, Cellarius. :)
Btw, shouldn't {vb:raw my_array.value1} be {vb:raw my_array.key1} in the third codebox ? (which would output "value1") |
Quote:
--------------- Added [DATE]1258449197[/DATE] at [TIME]1258449197[/TIME] --------------- There's two blog posts on this on vb.com: http://www.vbulletin.com/forum/entry...-4-based-files http://www.vbulletin.com/forum/entry...in-4-templates |
Quote:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Another note; on multidimensional arrays... Suppose you have a nested / multidimensional array set, like: PHP Code:
You can chain the array 'keys' like in the second example: HTML Code:
{vb:raw multiDim.test} <!-- Output: testval --> |
Hi,
How do i add a variable in form home template? Im trying to do this: Display stats on the forumhome yes/no Display chatbox stats on the forumhome. Don't forget to add the $mgc_cb_evo_stats variable in the FORUMHOME template where you want to display them otherwise they won't appear. Warning : Add 4 sql queries on the forumhome. For the users of a vBulletin version prior to 3.7.2, you will have to manually add the $mgc_cb_evo_stats var in the FORUMHOME template where you want the stats to appear. How do i get this done guys? Thanks in advance! |
You did not like my answer in the other thread?
Quote:
|
Quote:
I don't know about whether this is a rendering question or not. All i know is i have to alter something. Since you where kind enough to help me out by pointing me to this thread, i dropped the question here too, after reading what has been said up here. Now i just read that part where you pointed me out, it doesn't say what file i have to open and at what line i have to insert some code. Isn't that how it works? |
Sorry for being honest, but this is not how it works and it is quite obvious that you do not know what you are doing.
Of course "something" has to be done, but I do not know that mod, and therefore can not tell you what exactly will have to be done to make it vB4 compatible. It's not like there is a file in every mod that needs tampering that could be pointed out to you, where you do some mechanical search and replace and everything is fine. That's just not how it works, and this thread is not about updating the particular mod you are so interested in, but about helping coders to update their work. What has to be done is different for every single one of the hundreds of mods out there, and this article tries to provide just some of the many tools needed for that work. The author of that mod will have to update his work, and you will have to be patient until he does - just like anyone else. If he decides not to do that you can pay someone to do it, or you can do it yourself if you have the skills. If that is not sufficient to you, you may ask in the thread for that modification, but be prepared that mod authors will be annoyed if dozens of users spam their threads with questions that go "are you not yet done", "when will you be updating this" and stuff. Please read the announcement regarding this: https://vborg.vbsupport.ru/showthread.php?t=228073 |
Just wanted to thank you for this article.. I had been messing around on my own for a few hours on two of my mods that involved FORUMHOME and after reading your article (specially the last part) both my mods are now up and running smoothly on vb4 !!
CHEERS to Cellarius! |
Quote:
I'm not trying to do an update. Nor am I trying to port or convert the mod to vb4. I, myself uses vb 3.8.4. This mod has a few options. And one of the options out there tells you that if you activate 'this' option you have to add the $mgc_cb_evo_stats variable in the forumhome template. Althought its about a particular mod, but I got the impression that this is something that is outside the scope of the mods explanation, looking at the explanation on how to do it, which is rather 'short'. You misunderstood me, lets just leave it to that. Now if you don't mind, have a nice evening. |
Quote:
|
Just wanted to thank you guys both cellarius and Shadab, I've made a lot of progress on porting some major mods after reading what you have posted, I'm doing it for my own experience as I'm planning to finally make the move to vB after having the license for more than a year now lol (been running WBB2.x for ages)
hope that you guys to continue on posting more good stuff :) |
Very nice job.
I wasn't aware of pre-registering. :) Thanks. |
Quote:
|
Can you give us a simple example of how to insert code to footer without fech from other template?
|
Try to insert $my_insertvar
PHP Code:
|
hi cellarius, I don't want to call the var manually, I just want to inject code like my old example for 3.x:
hook: parse_templates code: $vbulletin->templatecache['footer'] .= 'text added to footer'; Capiche? |
Nope, that does not work. You would need to do a str_replace on something you know is there.
|
I figure out how to solve it:
hook: process_templates_complete code: $footer .= 'text added to footer'; No idea if was the best solution, but that worked very well. |
1 Attachment(s)
Quote:
PHP Code:
Attachment 106372 Using str_replace, you can also add stuff in the middle of the template like so: PHP Code:
|
Hi,
I'm trying to add something that requires PHP under the navbar. Back in vb 3.8.x I just used plugins for this. But, I'm not sure which hook to use if I want to display something under the navbar. |
@David: This is not related to the topic of this article. You should open your own thread on this..
Also, I just added the bonus track about template caching. |
Quote:
It's $cache[] = 'xxx'; and not $globaltemplate now;) ;) |
Ah, damn, of course you're right. I had my example code for vB3 and the one I was about to change for vB4 side by side and then obviously copied/pasted the wrong one. Stupid me :)
|
Quote:
I made the mistake in all my add-ons on porting them to vB4:D |
Thanks for the "cache your templates" info, I made use of this info after making my own Terms of Service and Privacy custom pages, very helpful, thank you.:up:
|
Quote:
<hookname>showthread_query</hookname> $vbulletin->templatecache['postbit'] = str_replace($find, $replace, $vbulletin->templatecache['postbit']); Also can this be used on variables? ex: {vb:raw variable} |
This isn't working for me. I'm probably doing something wrong, but at the moment I have a custom php page (I created following another tutorial elsewhere) that is calling a custom template.
The code for the php page: PHP Code:
And the HTML template (called TESTPAGE): HTML Code:
{vb:stylevar htmldoctype} PHP Code:
Where have I gone wrong? I've spent a good half an hour going through the tutorial and trying various things but can't seem to get it working. It always makes the whole site blank, but if I call another template (e.g. contactus instead of TESTPAGE) it works fine, so the problem seems to lie with the plugin. Any help greatly appreciated - thanks in advance :) |
In the plugin change "THEMEFLARE" to "TESTPAGE" ....
|
Quote:
Edit: I've updated my original post. |
I seemed to have gotten it to work. :D
Downloads.php PHP Code:
DOWNLOADS_SHELL Code:
{vb:raw headinclude} |
thank you very much..
|
Code:
eval( '$comment_boxes .= "' . fetch_template('App_Question_box') . '";' ) ; |
Quote:
PHP Code:
|
Alright, now I'm a bit stumped.
I cant seem to get the title of the page to register and it the pages don't work with subdomains. Anyone got a clue? |
Quote:
|
Alright, my bad. I got the title to work.
The bit about the subdomains does relate back to this. I've made a custom page using this How-To. When I load the page in the directory that global.php is in (the forum root directory) it loads absolutely fine. What I'm trying to do is made this template appear in a sub domain (stats.example.com) rather than being in the root (example.com) where the forums live. When I try running the code from the sub domain, it comes up and says that it doesn't know where "./global.php" is. I change this to be "http://example.com/global.php". That's fine, but then I get another error saying that it then can't find another .php file, etc, etc. I'm just trying to figure out how I can get my custom page to load on a sub domain. |
Ahm, just like I suspected.
Quote:
|
All times are GMT. The time now is 07:41 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|