vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 4 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=242)
-   -   [HOW TO - vB4] Rendering templates and registering variables - a short guide (https://vborg.vbsupport.ru/showthread.php?t=228078)

Sadikb 04-07-2010 10:34 AM

Just wanted to thank Cellarius for this very useful article. Thanks Mate...

caciocode 05-02-2010 02:47 PM

Where exactly do I write this content? This part is quite complicated

ckgreenman 05-03-2010 05:56 PM

Hello, I hope someone can point in the right direction. I'm trying to port a mod to vb4 which has a number of templates. The php file contains a whole bunch of
Code:

eval('$template .= "' . fetch_template('templatename') . '";');
lines. The mod uses a main template which then calls the other templates from within the main template using $template references to the other templates.

I've tried a number of different iterations form the first post but I can't get anything but the main template to render. How does one go about making something like this work?


thanks.

ckgreenman 05-04-2010 06:32 PM

nevermind. Turns out I had numerous typos.


Now I'm running into a different problem. I'm editing a (sub)template which is rendered in a variable in the php file and called using:
Code:

{vb:raw display_map}
The display_map template references a number of variables which are defined in the php script and populated with data pulled from the database. When I try to edit the template and use the {vb:raw variable} I'm unable to save the template due to the following error:
Code:

Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3332

vBulletin Message
The following error occurred when attempting to evaluate this template:
%1$s
This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.


Any suggestions where the problem might be? It doesn't seem to matter where I put the variable reference. If there is {vb:raw variable} in the template it won't save.

GameExploiters 05-07-2010 08:22 PM

Quote:

Originally Posted by cellarius (Post 1915072)
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

I'm trying to render my Navbar template so i can add it to my custom templates and be able to change the navbar how would i go about doing this with this tutorial. I don't understand what I'm suppose to do exactly.

Dark_Wizard 06-25-2010 05:13 PM

Quote:

Originally Posted by ckgreenman (Post 2031749)
nevermind. Turns out I had numerous typos.


Now I'm running into a different problem. I'm editing a (sub)template which is rendered in a variable in the php file and called using:
Code:

{vb:raw display_map}
The display_map template references a number of variables which are defined in the php script and populated with data pulled from the database. When I try to edit the template and use the {vb:raw variable} I'm unable to save the template due to the following error:
Code:

Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3332

vBulletin Message
The following error occurred when attempting to evaluate this template:
%1$s
This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.


Any suggestions where the problem might be? It doesn't seem to matter where I put the variable reference. If there is {vb:raw variable} in the template it won't save.

Chris...sent you a PM...

Bob

Frank T 07-01-2010 07:06 PM

I'm working to insert my own VAR into an existing template. I've seen the examples saying to use vb_Template::preRegister. However I have a catch...I'm using a cloned copy of an existing template. For example, I cloned the default style -- and created newStyle. Then within newStyle, I modified postbit_legacy and would like to display my own $var within the cloned copy of postbit_legacy.

Questions:
1. How to register/preRegister into a cloned template (newStyle->postbit_legacy).
2. How to properly use preRegister -- if that's what's needed. My data isn't an array, and preRegister only works with arrays.

cellarius 07-03-2010 07:40 AM

If you're creating a new style based on another one (in this case the default style), the template name is still postbit_legacy and the template itself still is template legacy. Doesn't matter at all.
If you're variable is not an array, make it an array. $templatevalues['myvariable'] = $myvariable.

SpeedyHire 07-20-2010 07:23 AM

Could you please point me in the right direction as to how I might add an extra field in newthread template, I was trying to do it automatically with the product install and I read here about using process_template hook and str_replace but Im still a little confused by it any help on this greatly appreciated.

cellarius 07-20-2010 07:25 AM

Please open your own thread for questions not directly related to this article.


All times are GMT. The time now is 03:35 PM.

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.01542 seconds
  • Memory Usage 1,814KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_code_printable
  • (3)bbcode_html_printable
  • (10)bbcode_php_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (4)pagenav_pagelink
  • (1)pagenav_pagelinkrel
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete