![]() |
|
Article Options |
[HOW TO - vB4] Rendering templates and registering variables - a short guide
by
![]() Introduction Starting with vB4, templates no longer get output using eval:
is outdated. 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
Note the last one: multidimensional arrays are perfectly possible. . . . Now, with the result of the rendering we can do several things: . Output template directly - custom pages
This immediatly outputs the template. Use this if you have created your own page, for example. Note the second line, which is special for this type of use:
This auto-registers the page level templates header, footer and headinclude that you will use in the template of your custom page. . Use a template hook
The template will be shown using the choosen template hook (for example: $template_hook[forumhome_wgo_pos2]). See the dot before the = in the last line? The hook may be used by other modifications, too, so we don't want to overwrite it, but rather append our code to it, conserving everything that might already be there. . Save into a variable for later use in custom template
Now we have saved the rendered template into a variable. This variable in turn we can later on register in another template, if we want:
Again, inside my_other_template we now could call
If you're running the first template call inside a loop, you may want to use .= instead of = in the last line, so that the results of every loop get added instead of overwriting the existing. But that depends, of course. . Save into an array and preregister to use in an existing/stock template
Note: it is not {vb:raw templatevalues.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:
Of course you could add further pairs to that array if you need to preregister more than one variable. . . 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:
. . 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 The following members like this post: LOGECT, Judge Dredd X, cassis2k, mokonzi, sadiq6210 and 13 others.
Last edited by cellarius; 20 Nov 2009 at 19:37.. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
Views: 106760
|
Comments |
#2
|
||||
|
||||
Great article, cellarius! Thanks for taking the time to write this out.
__________________
Former vBulletin.org Staff Member
Try a search before posting for help. Many users won't, and don't, help if the question has been answered several times before. W3Schools - Online vBulletin Manual If I post some CSS and don't say where it goes, put it in the additional.css template. I will NOT help via PM (you will be directed to post in the forums for help.) No members have liked this post.
|
#3
|
||||
|
||||
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
![]()
__________________
Please note that there will be no further updates to my addons, especially they will not be upgraded for vB5. I'm leaving vB, since IB choose to go the banana-way yet again. http://www.roma-antiqua.de No members have liked this post.
|
#4
|
||||
|
||||
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") No members have liked this post.
|
#5
|
||||
|
||||
Originally Posted by Shadab
Of course, thanks for pointing this out. Corrected.![]()
--------------- Added 17 Nov 2009 at 08:13 --------------- 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
__________________
Please note that there will be no further updates to my addons, especially they will not be upgraded for vB5. I'm leaving vB, since IB choose to go the banana-way yet again. http://www.roma-antiqua.de No members have liked this post.
Last edited by cellarius; 17 Nov 2009 at 08:13. Reason: Auto-Merged DoublePost |
#6
|
||||||||
|
||||||||
Not a problem!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Another note; on multidimensional arrays... Suppose you have a nested / multidimensional array set, like:
You can chain the array 'keys' like in the second example:
No members have liked this post.
|
#7
|
|||
|
|||
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! No members have liked this post.
|
#8
|
||||
|
||||
You did not like my answer in the other thread?
Originally Posted by cellarius
The mod clearly has not been updated for vB4. This very likely is not only a template rendering question, and this is not the place to discuss the porting of other peoples modifications. If you want to try anyway, the answer is in the article, look at the "Save into an array and preregister to use in an existing/stock template" part.
![]()
__________________
Please note that there will be no further updates to my addons, especially they will not be upgraded for vB5. I'm leaving vB, since IB choose to go the banana-way yet again. http://www.roma-antiqua.de No members have liked this post.
|
#9
|
|||
|
|||
Originally Posted by cellarius
![]()
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? No members have liked this post.
|
#10
|
||||
|
||||
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: http://www.vbulletin.org/forum/showthread.php?t=228073
__________________
Please note that there will be no further updates to my addons, especially they will not be upgraded for vB5. I'm leaving vB, since IB choose to go the banana-way yet again. http://www.roma-antiqua.de No members have liked this post.
|
#11
|
||||
|
||||
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!
__________________
Please Do Not PM me for vBulletin assistance... I am currently only doing customization & addon's for Xenforo. No members have liked this post.
|
#12
|
|||
|
|||
Originally Posted by cellarius
![]()
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. No members have liked this post.
|
#13
|
||||
|
||||
Then why in heaven do you post in an article that discusses vB4 programming techniques? If you need support for a mod the one and only place to go is the thread for this mod.
__________________
Please note that there will be no further updates to my addons, especially they will not be upgraded for vB5. I'm leaving vB, since IB choose to go the banana-way yet again. http://www.roma-antiqua.de No members have liked this post.
|
#14
|
|||
|
|||
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 ![]() No members have liked this post.
|
#15
|
||||
|
||||
Very nice job.
I wasn't aware of pre-registering. ![]() Thanks.
__________________
No members have liked this post.
|
![]() |
«
Previous Article
|
Next Article
»
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Article Options | |
|
|
New To Site? | Need Help? |
All times are GMT. The time now is 22:26.