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)

Boofo 04-02-2011 06:12 AM

Quote:

Originally Posted by TalkVirginia (Post 2179676)
I'm trying to capture a rendered/parsed template to a variable like so:

PHP Code:


$templater 
vB_Template::create('test');
$templater->register('userid'$userid);
$templater->register('bbtitle'$bbtitle);
$templater->register('homeurl'$homeurl);
$templater->register('forumurl'$forumurl);
$myvar $templater->render(); 

echo 
$myvar

I would expect that myvar would contain the parsed template, but it comes up empty.

Can anyone help me?

This:

Code:

$myvar = $templater->render();

should be this:

Code:

$myvar .= $templater->render();

rlarner 04-05-2011 07:49 PM

I'm struggling with getting data from a variable into one of the stock templates. Here's what I've done:
Created a custom plugin with the hook location set to "global_start":
$roscohtml = "<div>HELLO WORLD!</div>";
error_log($roscohtml);
vB_Template::preRegister('header',array('roscohtml '=>$roscohtml));

Then I customized the header template to contain this:
<div class="above_body"> <!-- closing tag is in template navbar -->
<div>test before</div>
{vb:raw roscohtml}
<div>test after</div>

Unfortunately the variable isn't being output. I do see the contents in the error log, so I know it's firing.

Any tips?

Thanks,
Rosco

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

OK, I think I found the problem - the global_start hook is not the right place for this to live. I moved my code to the parse_templates hook and the data is now available.

Is there a document which outlines the best way to leverage the hooks, or at least where each is called in the flow?

Rosco

bela-meaad 04-25-2011 06:42 PM

as templates no longer get output using eval
i tried to change it but seems not working.

can anyone help me with this code?


PHP Code:

if ($vbulletin->options['card_onoff'] AND THIS_SCRIPT != 'private' AND THIS_SCRIPT != 'login')
{
if( 
$vbulletin->userinfo['usergroupid']!=OR $vbulletin->userinfo['usergroupid']!=OR $vbulletin->userinfo['usergroupid']!=)
{
    
$istherewinner=mysql_query("SELECT `value` FROM `setting` WHERE `varname` = 'card_carintwin' LIMIT 1");
    while (
$yeswinner $db->fetch_array($istherewinner))
{
    
$bigwinner =$yeswinner[value];
}

if(
$bigwinner)
{
    eval(
'$card_win_index = "' fetch_template('card_win_index') . '";');
    
$ad_location['global_below_navbar'] .= $card_win_index;
}
else
{
    eval(
'$card_index = "' fetch_template('index_card') . '";');
    
$ad_location['global_below_navbar'] .= $card_index;
}
}



Boofo 05-07-2011 02:48 PM

<a href="https://vborg.vbsupport.ru/showpost.php?p=2180014&postcount=151" target="_blank">https://vborg.vbsupport.ru/showp...&postcount=151</a>

BirdOPrey5 07-05-2011 07:09 PM

I hoping this isn't too far off topic bt if possible I think it would make a good addition to the article...

What if I want to override an existing template with a new template. Basically ignore whatever is in a given template and render a new template, and copy the new template to the old template? Is that possible?

I've tried stuff like:

Code:

$templater = vB_Template::create('my_new_template');
$templater->register('myvar', $myvar);
$globaltemplates['existing_vb_template'] = $templater->render();

in all the various template hooks to no avail. I've also tried using $vbulletin->templatecache['existing_vb_template'] in the last line instead of $globaltemplates and also did not work.

Is what I'm asking possible? Seems like it should be... Any input would be appreciated.

Boofo 07-05-2011 07:58 PM

Yes, it can be done easily, if what you are wanting is what I understand it to be. As an example, here is what I did for that:

Code:

$templater = vB_Template::create('boofo_forumhome_event');
    $templater->register('callink', $callink);
    $templater->register('daysevents', $daysevents);
    $templater->register('eventdate', $eventdate);
    $templater->register('eventdates', $eventdates);
$upcomingevents .= $templater->render();

$vbulletin->templatecache['forumhome_event'] = $vbulletin->templatecache['boofo_forumhome_event'];


BirdOPrey5 07-05-2011 09:40 PM

Great, Thanks Boofo!

It might be worth mentioning for anyone else- you have to register any variables used to the ORIGINAL template (the one you are overriding) not the new template... At least that's the only way it's working for me. (On parse_templates hook)

Boofo 07-05-2011 10:12 PM

Are you sure? I used the same variables when I did it but I would think you only need to pre-register the variables you are going to use, not the other way around.

I did the above code in forumhome_start. Did you try the process_templates_complete hook?

BirdOPrey5 07-06-2011 12:34 AM

Maybe I didn't explain it right... in your example registering callink and the others to boofo_forumhome_event didn't do anything for me- I had to preRegister them to forumhome_event for them to work. I tried both parse_templates and process_templates_complete hooks.

To me it makes sense since the forum thinks it's outputting forumhome_event, and this is confirmed by viewing template names in the HTML source code.

Boofo 07-06-2011 12:53 AM

Well, yes and no. Since I did it in the forumhome_start hook, it needed those pre-registered for my new template. As long as the variables are good at that hook, then it will work the way I did it.

The forumhome doesn't care about the old template as long as I pre-register everything for my new template. Doing it in the parse_templates hook might be why it didn't work for you with your code. You would have to check whether whatever variables you are pre-registering have already been validated at whatever hook you are using.

Now I'm getting confused. And it hurts! LOL


All times are GMT. The time now is 06:54 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.02068 seconds
  • Memory Usage 1,762KB
  • 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
  • (4)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (4)pagenav_pagelink
  • (2)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