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 |
#232
|
|||
|
|||
Oh, and is there any way to show all my array cells If I don't know which size is the array?
Edited: I suspect that it is done in this way. Code:
<vb:each from="my_array" key="key1" value="my_result"> {vb:var my_result} </vb:each> resource(46) of type (mysql result) And each row is: Code:
array(21) { ["id"]=> string(2) "18" ["name"]=> string(5) "ABRIR" ["description"]=> string(23) "Iniciar la interacci?n." ["userid"]=> string(4) "2858" ["username"]=> string(20) "Seducci?n Cient?fica" ["dateline"]=> string(10) "1311875182" ["lastupdate"]=> string(10) "1311933838" ["categoryid"]=> string(1) "1" ["status"]=> string(1) "1" ["ipaddress"]=> string(13) "81.202.205.41" ["attach"]=> string(1) "0" ["threadid"]=> string(1) "0" ["lastupdater"]=> string(20) "Seducci?n Cient?fica" ["lastupdaterid"]=> string(4) "2858" ["tags"]=> string(16) "abrir,escalada 1" ["popup"]=> string(23) "Iniciar la interacci?n." ["views"]=> string(1) "0" ["votenum"]=> string(1) "0" ["votetotal"]=> string(1) "0" ["linkurl"]=> string(7) "http://" ["banner"]=> string(7) "http://" } Code:
<vb:each from="my_query" value="sentence"> Test {vb:raw sentence.name} </vb:each> But It doesn't show nothing, as if the each is not being executed. I expected to show, at least, 46 Test words. |
#233
|
|||
|
|||
Quote:
You are registering the return from one of the query functions. You need to call fetch_array() to get an array for each row, like: Code:
$results = $db->query_read("some sql"); while ($row = $db->fetch_array($results)) { // do something with $row } Of course each row is an array (even if you only selected one column). If you are selecting one column from a number of matching rows, you won't get an array with all the matching values, you still get an array for each row. If you want an array with all the matching values you'd have to build that yourself. Note also that you don't *have* to register an array to your template and use vb:each - you could format your own html string in the while loop above, then just register the string. |
#234
|
|||
|
|||
[QUOTE=Boofo;2287132]Try this in the parse_templates hook:
Boofo, I was working on specialized routine and trying to use some of the vb-ojects to solve my problem when I came across the code you posted here. This redirected me to the right vb routines. Thanks so much for sharing! |
#235
|
|||
|
|||
I have a problem on a page. I can use the variable {vb:raw companyname} in the places of the template that I need to. However when I want to place it in my navbits which are created by the php file it just echos itself.
Code:
$navbits = construct_navbits(array('' => ' </span></li><li class="navbit"><span><a href="/">Home</a></span></li><li class="navbit"><span><a href="../hostindex.php">Web Hosting</a></span></li><li class="navbit lastnavbit"><span>{vb:raw companyname} ')); $navbar = render_navbar_template($navbits); // ###### YOUR CUSTOM CODE GOES HERE ##### $pagetitle = '$companyname'; $templater = vB_Template::create('headerincludea'); $headerincludea = $templater->render(); $templater = vB_Template::create('webhost-php'); $templater->register('companyname', $companyname); $templater->render(); $templater->register_page_templates(); $templater->register('navbar', $navbar); $templater->register('pagetitle', $pagetitle); $templater->register('headerincludea', $headerincludea); print_output($templater->render()); |
#236
|
|||
|
|||
Try putting $companyname in your navbits instead of {vb:raw companyname} Since it's in a single-quoted string you need to use concatenation, like:
Code:
$navbits = construct_navbits(array('' => ' </span></li><li class="navbit"><span> <a href="/">Home</a></span></li><li class="navbit"><span> <a href="../hostindex.php">Web Hosting</a></span></li> <li class="navbit lastnavbit"><span>' . $companyname . ' ')); |
#237
|
|||
|
|||
Quote:
|
#238
|
|||
|
|||
Can anyone help me out what i'm doing wrong?
i'm trying to add something to the Default navbar template. (a server status for my game server) but first i'm trying out the example but i can't get it to work. This is my plugin setup. Product : vBulletin Hook location: Global_start Title: server Status execution order :5 Plugin code: Code:
/* Some Code, setting variables, (multidimensional) array */ $my_var = "abc"; /* render template and register variables */ $templater = vB_Template::create('navbar'); $templater->register('my_var', $my_var); $templater->render(); Code:
{vb:raw my_var} Code:
<p>{vb:raw my_var}</p> Edit: and Enable Plugin/Hook System is enabled also |
#239
|
|||
|
|||
You don't want to render the navbar template because the vbulletin code already does that for you. To get your variable registered to that template you want to use preRegister, like;
Code:
$my_var = "abc"; vB_Template::preRegister('navbar', array('my_var' => $my_var)); |
Благодарность от: | ||
Chr1sj |
#240
|
|||
|
|||
Thanks a lot. :up:
Now i can start creating my plugin understanding the preRegister. |
#241
|
|||
|
|||
Quote:
I'm doing this using $GLOBALS[templatevalues] = $templatevalues; rather than preRegister(). Code:
$templater = vB_Template::create('layout_start'); $templater->register('my_var', $my_var); $templatevalues['start_insertvar'] = $templater->render(); $GLOBALS[templatevalues] = $templatevalues; UPDATE: (Thanks to kh99 for his guidance.) The following line isn't needed and was used incorrectly: $template->register_global('templatevalues'); Then use this in the templates where you want the output to appear: Code:
{vb:raw GLOBALS.templatevalues.start_insertvar} |
Благодарность от: | ||
BirdOPrey5 |
Thread Tools | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|