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)

Lynne 12-10-2009 01:53 PM

Quote:

Originally Posted by cellarius (Post 1928317)
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! :)

cellarius 12-10-2009 03:06 PM

Quote:

Originally Posted by Lynne (Post 1928334)
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 :D

Quote:

Good morning, er, evening cellarius! :)
And to you, Lynne - whatever suits our needs :)

Nuss 12-12-2009 11:38 PM

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)

James Birkett 12-13-2009 07:47 PM

Do we need to register stylevars as well?

MaestroX 12-14-2009 01:29 AM

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

nubian 12-21-2009 02:38 AM

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. :o

Thanks for this write up, It helped me quite a bit in understanding more and more about VB.

cellarius 12-21-2009 04:21 AM

Quote:

Originally Posted by nubian (Post 1934363)
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 :D

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.

nubian 12-21-2009 06:19 AM

Quote:

Originally Posted by cellarius (Post 1934398)
Too much information :D

It was a lot better than reading video game manuals. :)
Quote:

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

CypherSTL 12-24-2009 04:20 PM

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') . '";');

Lynne 12-24-2009 04:41 PM

Quote:

Originally Posted by CypherSTL (Post 1937378)
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();


CypherSTL 12-28-2009 05:46 PM

I still cannot figure out how in the heck to work this new template system.

Code:

eval('$awarduserslist .= ", ' . fetch_template('awards_awardusers_bit') . '";');
Works flawlessly. No errors.

However
Code:

$displayTemplate = vB_Template::create('awards_awardusers_bit');
$awarduserslist .= $displayTemplate->render();

Displays absolutely NOTHING.

scarex 12-29-2009 10:46 AM

Thanks for this guide, at the beginning I was totally confused about this new system.

On the other hand, is there a system to preregister a var in all templates with a single call?

IR15H 12-29-2009 11:51 PM

Thanks for the guide :)

rbc 12-31-2009 12:52 PM

Quote:

Originally Posted by cellarius (Post 1915072)
Introduction
~snip~

@ cellarius

Absolutly a great introduction how things now working on VB4.
It answere`s many questions to me now.

I just wanne thank you cellarius for this.

btw. many thanks to Lynne who also allways help out.

i`m absolutly new to stuff like "vb" or "php" but with your "helping
hand`s" alot of work is possible to do for me too.

Sorry about my englisch, its not "my one" :)

Thanks again an enjoy the hollydays ...... if there some ........

NLP-er 01-06-2010 12:32 AM

I have question:
How to change content of existing template inside of plugin?

I'm remaking my mod for vB4. I'm adding there flags to header of footer. In vB3 version I simply change insides of template using templatecache. I.e.:
PHP Code:

$vbulletin->templatecache['footer'] .= 'ADDITIONAL TEXT IN FOOTER'

I was using it in global_start hook, but it doesn't work anymore - $vbulletin->templatecache['footer'] is empty and have no impact on footer.

How to change this line of code to make it working in vB4?

EDIT:
Ok - I already found it here :) Now check if its working :P

--------------- Added [DATE]1262746169[/DATE] at [TIME]1262746169[/TIME] ---------------

Other question:
In vB3 my mod have possibility to put additional data in custom place - so user just manually adds variable into required template and he has flags where he put it. How to do this in vB4 where variables have to be preregistered????...

--------------- Added [DATE]1262746545[/DATE] at [TIME]1262746545[/TIME] ---------------

Quote:

Originally Posted by testebr (Post 1916821)
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.

This will work only for few templates. The question is how to make it work for any template like with $vbulletin->templatecache solution in vB3.

Also this solution is working on already parsed template - I need fresh one, not parsed yet. Anyone have idea how to do that?

EDIT
Ok I have it :) need to use hook parse_templates

Sarcoth 01-07-2010 05:22 PM

Awesome guide. It has helped me move forward a bit, but I still don't have it all down yet. If anyone has this down pat, I'd be interested in some more tutorials that show old 3.x code and then below shows the 4.x code. This way I can test myself and see how well I have it down.

In the meantime though, is the new rendering needed for redirects? For example:
Code:

$vbulletin->url = "misc.php?do=editform&fid=$fid";
eval(print_standard_redirect('redirect_insertform', true));

I tried the following:
Code:

$templater = vB_Template::create('redirect_insertform');
        $templater->register_page_templates();
        $templater->register('redirect', $vbulletin->url);
print_standard_redirect($templater->render());

That didn't work though, it seems to be cutting off everything after misc.php. Maybe I need to register the $fid?

cellarius 01-07-2010 07:28 PM

No, standard redirects and errors still work the old way. If you want to know something like that, just look one up in the original vB4 php files.

Abe Babe 01-12-2010 06:26 AM

I have a template that I need to insert into multiple pre-existing templates (I add some additional graphics/formatting to the header and footer of most tables ... and my CSS skills aren't good enough to achieve what I'm trying to do through CSS alone, so I need to add old HTML table coding). I do it this way so that if I want to make changes, I don't have to change lots of different templates. After a bit of struggling, I have managed to get the following code running.

Code:

$templater = vB_Template::create('layout_start');
    $templater->register('my_var', $my_var);
$templatevalues['start_insertvar'] = $templater->render();
vB_Template::preRegister('FORUMHOME', $templatevalues);


I have two questions. Is there any way to preRegister for more than one pre-existing template (or a global registration), or do I have to create Plugins for every page I want to add this to (*groan*)? And what is the best hook to have this on. I am currently using 'parse_templates'.

The other unusual thing I'm finding happening is if I include my template in postbit_legacy, it will show on the first post, but not on the posts after that.

Thanks in advance...

cellarius 01-13-2010 01:16 AM

Quote:

Originally Posted by Abe Babe (Post 1953310)
Code:

$templater = vB_Template::create('layout_start');
    $templater->register('my_var', $my_var);
$templatevalues['start_insertvar'] = $templater->render();
vB_Template::preRegister('FORUMHOME', $templatevalues);

I have two questions. Is there any way to preRegister for more than one pre-existing template (or a global registration), or do I have to create Plugins for every page I want to add this to (*groan*)?

Just calling the preregister method as often as you need it might not work in this case, since the method clears the variable. So trying to simply preregister it again might not be feasible. Anyway, try this:
Code:

$templater = vB_Template::create('layout_start');
    $templater->register('my_var', $my_var);
$templatevalues['start_insertvar'] = $templater->render();
vB_Template::preRegister('FORUMHOME', $templatevalues);
vB_Template::preRegister('SHOWTHREAD', $templatevalues);
vB_Template::preRegister('FORUMDISPLAY', $templatevalues);

If this does not work, try something like this:
Code:

$templater = vB_Template::create('layout_start');
    $templater->register('my_var', $my_var);
$start_insertvar = $templater->render();
$templatevalues['start_insertvar'] = $start_insertvar;
vB_Template::preRegister('FORUMHOME', $templatevalues);
$templatevalues['start_insertvar'] = $start_insertvar;
vB_Template::preRegister('SHOWTHREAD', $templatevalues);
$templatevalues['start_insertvar'] = $start_insertvar;
vB_Template::preRegister('FORUMDISPLAY', $templatevalues);

Of course, if you have really many templates, it might be more elegant to solve this with an array an a nice loop.
Quote:

And what is the best hook to have this on. I am currently using 'parse_templates'.
Seems fine to me.

Ted S 01-16-2010 05:19 PM

...

psypher 01-18-2010 08:13 PM

nvm figured it out

mokujin 01-19-2010 08:45 PM

I have read all posts and dont know how to call 3 templates in one page.

I have this code:

PHP Code:

if ($_REQUEST['do'] == 'page1')
{
  
$templater vB_Template::create('temp_page1');
  
    
/* 
    how can I  fetch another template here? 
    in vb3: eval('$pagebit .= "' . fetch_template('page_bit') . '";');     ???
    
    */

  
$mypage .= $templater->render();
}
 
// called 2 templates 'temp_page1' and 'temp_home' OK, and what  'page_bit'?
    
$templater vB_Template::create('temp_home');
$templater->register_page_templates();
$templater->register('mypage'$mypage);
$templater->register('navbar'$navbar);
$templater->register('pagetitle'$pagetitle);
print_output($templater->render()); 


derfelix 01-21-2010 04:04 AM

I would fetch it before...

PHP Code:

if ($_REQUEST['do'] == 'page1')
{

  
$templater vB_Template::create('page_bit');
 
  
$mypage_bit .= $templater->render();

  
$templater vB_Template::create('temp_page1');
  
$templater->register('mypage_bit'$mypage_bit);
  
$mypage $templater->render();
}

    
$templater vB_Template::create('temp_home');
$templater->register_page_templates();
$templater->register('mypage'$mypage);
$templater->register('navbar'$navbar);
$templater->register('pagetitle'$pagetitle);
print_output($templater->render()); 


F.

ChopSuey 01-22-2010 10:29 PM

Thanks for this, hopefully i'll be able to know what to do.

TheSupportForum 01-25-2010 05:56 AM

does anyone know how i can insert this into a template using plugin manager
to be placed in header

Code:


function bb2_insert_head() {
 global $bb2_javascript;
 echo $bb2_javascript;
}


OcR Envy 01-25-2010 02:30 PM

n/m I figured I was missing $templater->render();

moonray 02-09-2010 04:21 PM

I am not much of a coder at all, and hence need your help big time.

I want to call a simple php file to be placed just under the navbar. So, I created a plugin:
Product: vBulletin
Title: Insert Simple PHP
Execution order: 5 (it was default)
Hook Location: global_start
PHP code:
Code:

ob_start();
include('simple.php');
$insert_simple_php = ob_get_contents();
ob_end_clean();

Plugin is active: Yes

Now, I went to the NAVBAR template and inserted
Code:

{vb:raw insert_simple_php}
just under the code
Code:

{vb:raw ad_location.global_below_navbar}
Questions:
* Where does the 'register/pre-register' come?
* What is the exact change I need to make here?

Thanks for your help!

FatalBreeze 02-17-2010 04:29 PM

Great article!
but i have a problem.
I have a template which i want to show inside another template, and it seems to work with every template except for the header.
This is the plugin i used:
PHP Code:

$templateH vB_Template::create('TopPanel')->render();
vB_Template::preRegister('header', array('TopPanel' => $templateH));
$TL vB_Template::create('TopPanel')->render();
vB_Template::preRegister('vbcms_page', array('TopPanel' => $TL));
$TL2 vB_Template::create('BarH')->render();
vB_Template::preRegister('vbcms_page', array('BarT' => $TL2)); 

the hook location is: process_templates_complete.
of course i have a template called TopPanel which i created, and it works great in vbcms_page for example but not in header.
Do you have an idea why is that?

Thanks in advance.

cellarius 02-18-2010 05:45 AM

As the name suggests, the hook you use is called when vB-internal template processing is already completed. No use in preregistering templates then anymore. Since that hook did not exist in 4.0.1 as far as I can see, you're using 4.0.2. Another hook that got added in 4.02 is template_register_var, and that would be the right one to use.

cellarius 02-23-2010 07:19 AM

Quote:

Originally Posted by moonray (Post 1978401)
I am not much of a coder at all, and hence need your help big time.

I want to call a simple php file to be placed just under the navbar. So, I created a plugin:
Product: vBulletin
Title: Insert Simple PHP
Execution order: 5 (it was default)
Hook Location: global_start
PHP code:
Code:

ob_start();
include('simple.php');
$insert_simple_php = ob_get_contents();
ob_end_clean();

Plugin is active: Yes

Now, I went to the NAVBAR template and inserted
Code:

{vb:raw insert_simple_php}
just under the code
Code:

{vb:raw ad_location.global_below_navbar}
Questions:
* Where does the 'register/pre-register' come?
* What is the exact change I need to make here?

Thanks for your help!

Try this in the plugin:
PHP Code:

ob_start();
include(
'simple.php');
$templatevalues['insert_simple_php'] = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('navbar'$templatevalues); 


Muller2 02-23-2010 09:43 PM

cellarius & moonray - you are both stars!!! :D

Thanks you!

Al

moonray 02-24-2010 12:41 AM

FINAL WORKING CODE:

Product: vBulletin
Title: Insert Simple PHP
Execution order: 5
Hook Location: global_start
PHP code:
Code:

ob_start();
include('simple.php');
$simple_php = ob_get_contents();
ob_end_clean();

vB_Template::preRegister('navbar',array('simple_php' => $simple_php));

Plugin is active: Yes

Now, go to the NAVBAR template and insert
Code:

{vb:raw insert_simple_php}
just under the code
Code:

{vb:raw ad_location.global_below_navbar}

TalkVirginia 02-25-2010 12:12 PM

I'm hoping someone can help me.. I'm having trouble displaying Openx Ads in various locations. For example in the header template.

This is the php code that Openx generates:
PHP Code:


//<!--/* OpenX Local Mode Tag v2.8.4 */-->

// The MAX_PATH below should point to the base of your OpenX installation
define('MAX_PATH''path/to/openx');

if (@include_once(
MAX_PATH '/www/delivery/alocal.php')) 
{

    if (!isset(
$phpAds_context)) 
    {
        
$phpAds_context = array();
    }

    
$phpAds_raw view_local(''100'_blank'$forumid'0'$phpAds_context'');

                echo 
$phpAds_raw[html];


This is the code I'm trying to use:

PHP Code:


//<!--/* OpenX Local Mode Tag v2.8.4 */-->

// The MAX_PATH below should point to the base of your OpenX installation
define('MAX_PATH''/path/to/openx');

if (@include_once(
MAX_PATH '/www/delivery/alocal.php')) 
{
                
ob_start();

    if (!isset(
$phpAds_context)) 
    {
        
$phpAds_context = array();
    }

    
$phpAds_raw view_local(''100'_blank'$forumid'0'$phpAds_context'');

    
$templater vB_Template::create('ad_global_header2');

                
$templater->register('phpAds_raw'$phpAds_raw);

                
$templatevalues['phpAds_raw'] = ob_get_contents();

                
ob_end_clean();

                
vB_Template::preRegister('header'$templatevalues);




TalkVirginia 02-26-2010 03:25 PM

No comments on my last post?

wajones 02-28-2010 08:02 AM

This is what I'm using.
PHP Code:

ob_start();

if (include(
'ads/phpadsnew.inc.php')) {
        if (!isset(
$phpAds_context)) $phpAds_context = array();
        
$sponsor view_raw ('6'7'_blank''''0'$phpAds_context); 
       
    }

ob_end_clean();
vB_Template::preRegister('navbar',array(
'sponsor' => $sponsor)); 


TalkVirginia 02-28-2010 04:24 PM

Thank you very much WAJones!! That works great! :D :up: :up: :up:

masterross 03-01-2010 07:49 PM

Guys help me to update this hack to vB 4
https://vborg.vbsupport.ru/misc.php?...t_post_only_37

I cant show the message "register...." to guests:

PHP Code:

if ($show['guest'] AND $forum['gfpo_enabled'])
{
    eval(
'$postbit = "' fetch_template('postbit_gfpo') . '";');
    eval(
'$postbits .= "' fetch_template('postbit_wrapper') . '";');



Michael Biddle 03-01-2010 09:03 PM

Great article!

I have a question. In vB3, you could do this:

PHP Code:

$template_hook['navbar_buttons_left'] .= '<td class="vbmenu_control"><a href="index.php">Home Page</a></td>'

Is it possible to just have the html in the plugin, or does it need to be in its own template in vb4?

masterross 03-01-2010 09:49 PM

Quote:

Originally Posted by Michael Biddle (Post 1994751)
Great article!

I have a question. In vB3, you could do this:

PHP Code:

$template_hook['navbar_buttons_left'] .= '<td class=&quot;vbmenu_control&quot;><a href=&quot;index.php&quot;>Home Page</a></td>'

Is it possible to just have the html in the plugin, or does it need to be in its own template in vb4?

It's absolutely the same in vB 4 only hooks are different.

TalkVirginia 03-03-2010 09:34 PM

I'm trying to append some text to the end of the footer.. in a plugin.. but I'm totally lost.

How do I get the footer out of the cache to append some text to it?


All times are GMT. The time now is 11:42 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01756 seconds
  • Memory Usage 1,951KB
  • 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
  • (23)bbcode_code_printable
  • (15)bbcode_php_printable
  • (16)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (3)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (40)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