Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
[HOW TO - vB4] Rendering templates and registering variables - a short guide
cellarius's Avatar
cellarius
Join Date: Aug 2005
Posts: 1,987

 

Show Printable Version Email this Page Subscription
cellarius cellarius is offline 11-15-2009, 10:00 PM

Introduction

Starting with vB4, templates no longer get output using eval:
PHP Code:
eval('$mytemplate = "' fetch_template('mytemplate') . '";'); 
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

PHP Code:
/* Some Code, setting variables, (multidimensional) array */
$my_var "abc";
$my_array = array(
        
'key1' => 'value1',
        
'key2' => array(
                '
key21' => 'value21',
                '
key22' => 'value22'
        '
)
    );

/* render template and register variables */
$templater vB_Template::create('mytemplate');
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
$templater->render(); 
  • The first line provides the template that is to be rendered, using the new vB_Template class (vB_Template::create). The method gets passed the name of the template as an argument.
  • The following two lines register a variable and an array that we want to use in our template. Arguments passed are 1. the name you want to use to access the variable, and 2. the variable from the code you want to register. You can register as many variables/arrays as you want. Just remember you have to register every variable and array that you want to use in your custom template in this way. If you don't register them, they will not be available.
  • The fourth line renders the template ($templater->render()).
In the template you know will be able to use the registered variables/arrays in this way:
HTML Code:
{vb:raw my_var}
{vb:raw my_array.key1}
{vb:raw my_array.key2.key21}
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


PHP Code:
$templater vB_Template::create('mytemplate');
    
$templater->register_page_templates(); 
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
print_output($templater->render()); 
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:
PHP Code:
$templater->register_page_templates(); 
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


PHP Code:
$templater vB_Template::create('mytemplate');
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
$template_hook[forumhome_wgo_pos2] .= $templater->render(); 
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
PHP Code:
$templater vB_Template::create('mytemplate');
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
$mytemplate_rendered $templater->render(); 
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:
PHP Code:
$templater vB_Template::create('my_other_template');
     
$templater->register('my_template_rendered'$my_template_rendered);
 
print_output($templater->render()); 
Again, inside my_other_template we now could call
HTML Code:
{vb:raw my_template_rendered}
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

PHP Code:
$templater vB_Template::create('mytemplate');
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
$templatevalues['my_insertvar'] = $templater->render();
vB_Template::preRegister('FORUMHOME'$templatevalues); 
  • This is another, more flexible method to save the rendered template into a variable for future use in an already existing template. In this example, we want to show our own rendered template on forumhome.
  • Problem is: We have no direct way to register variables for already existing templates like FORUMHOME. It's created and rendered in the files, and we don't want to mess there.
  • To help with this, a new method was created for vB_Template class, called preRegister. Using this, we can pass our data to FORUMHOME before it is rendered. Note that the data needs to be saved into an array ($templatevalues['my_insertvar']), a simple variable will throw an error. In the last line the array is preregistered; you need to pass as arguments 1. the name of the existing template and 2. the array that contains the data. Again, this can be done for as many arrays as needed.
  • Of course, the preRegister functionality can be used for any kind of variables or arrays, no matter whether you have saved a rendered template (like in our example) into it or it contains just a simple boolean true/false statement.
To access the data inside the template it was preregistered for use:
HTML Code:
{vb:raw my_insertvar}
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:
PHP Code:
$my_insertvar $templater->render();
vB_Template::preRegister('FORUMHOME',array('my_insertvar ' => $my_insertvar)); 
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:
PHP Code:
// for a single template
$cache[] = 'mytemplate'

// for more than one templates in one step
$cache array_merge((array)$cache,array(
    
'mytemplate',
    
'myothertemplate',
    
'mythirdtemplate'
)); 
.
.
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
Reply With Quote
  #42  
Old 12-10-2009, 01:53 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by cellarius View Post
Ahm, just like I suspected.

This tutorial is not about making custom pages, and has nothing to do with your problem. This one is.
Actually, since he mentions global.php, I suspect it is actually this one.

Good morning, er, evening cellarius!
Reply With Quote
  #43  
Old 12-10-2009, 03:06 PM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
Actually, since he mentions global.php, I suspect it is actually this one.
Darn, your right of course - and obviously there's more than one person who should read thread titles more carefully

Quote:
Good morning, er, evening cellarius!
And to you, Lynne - whatever suits our needs
Reply With Quote
  #44  
Old 12-12-2009, 11:38 PM
Nuss Nuss is offline
 
Join Date: Apr 2004
Posts: 6
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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);
Later went to header template and write {vb:raw contenedor} at the first line, and cant get "Hi" (get nothing)
Reply With Quote
  #45  
Old 12-13-2009, 07:47 PM
James Birkett James Birkett is offline
 
Join Date: Jun 2009
Posts: 633
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Do we need to register stylevars as well?
Reply With Quote
  #46  
Old 12-14-2009, 01:29 AM
MaestroX MaestroX is offline
 
Join Date: Aug 2006
Location: UK
Posts: 249
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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);
The template is called into the header template but for some reason the navbit array doesn't seem to be working?

Thanks
Reply With Quote
  #47  
Old 12-21-2009, 02:38 AM
nubian nubian is offline
 
Join Date: Nov 2004
Posts: 117
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
$templater vB_Template::create('my_other_template');
     
$templater->register('my_template_rendered'$my_template_rendered);
 
print_output($templater->render()); 
I used this to fix it...
PHP Code:
$templater vB_Template::create('my_other_template');
     
$templater->register('my_template_rendered'$mytemplate_rendered);
 
print_output($templater->render()); 
Then sure enough it worked!

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:
$config['Misc']['debug'] = true
to the wrong config.php file.

Thanks for this write up, It helped me quite a bit in understanding more and more about VB.
Reply With Quote
  #48  
Old 12-21-2009, 04:21 AM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nubian View Post
Thank you cellarius for this!
I've even printed this out and read this even while sitting on the throne quite a few times.
Too much information

Quote:
The option to - Save into a variable for later use in custom template works only after noticing an error in this code.
There's no error in the code. You just changed the name of ine variable, and of course you are free to do so. The variables are just examples, you can name them whatever you want.


Quote:
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.
The hook is there, if not, you have customized your FORUMHOME template and removed it. Anyway, this also is just an example; it works the same with any given template hook. Plus, I don't know why you think this belongs to the section where I write about preregistering - if you use a template hook, you don't need to preregister.
Reply With Quote
  #49  
Old 12-21-2009, 06:19 AM
nubian nubian is offline
 
Join Date: Nov 2004
Posts: 117
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by cellarius View Post
Too much information
It was a lot better than reading video game manuals.
Quote:
Originally Posted by cellarius View Post
There's no error in the code. You just changed the name of ine variable, and of course you are free to do so. The variables are just examples, you can name them whatever you want.
Yes, I'm aware of changing the variables to anything I want.
At first I was using your example to familiarize myself on how things work.
PHP Code:
$templater vB_Template::create('mytemplate');
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
$mytemplate_rendered $templater->render();  

$templater vB_Template::create('my_other_template');
     
$templater->register('my_template_rendered'$my_template_rendered);
 
print_output($templater->render()); 
The code above gave me a blank page because the var $mytemplate_rendered = $templater->render();
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:
Originally Posted by cellarius View Post
The hook is there, if not, you have customized your FORUMHOME template and removed it. Anyway, this also is just an example; it works the same with any given template hook. Plus, I don't know why you think this belongs to the section where I write about preregistering - if you use a template hook, you don't need to preregister.
Pardon my ignorance since I'm still in developing stages of learning the functionality of how vb operates.
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.
Reply With Quote
  #50  
Old 12-24-2009, 04:20 PM
CypherSTL CypherSTL is offline
 
Join Date: Mar 2006
Location: St. Charles, MO
Posts: 306
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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') . '";');
Reply With Quote
  #51  
Old 12-24-2009, 04:41 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by CypherSTL View Post
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') . '";');
Code:
$newTemplate = vB_Template::create('userslist_bit');
    $newTemplate->register('variable1', $variable1);
    $newTemplate->register('variable2', $variable2);
$userslist .= $newTemplate->render();
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:16 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05460 seconds
  • Memory Usage 2,415KB
  • Queries Executed 26 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_code
  • (3)bbcode_html
  • (14)bbcode_php
  • (10)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (4)pagenav_pagelink
  • (1)pagenav_pagelinkrel
  • (11)post_thanks_box
  • (17)post_thanks_box_bit
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete