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 |
#242
|
||||
|
||||
I am creating a custom mod for my site, at the moment i have done this and only get a white page displayed, but at least the result appears. I am not sure what example to follow so that the rest of the showthread template appears too?
Plugin: Product: vBulletin Hook Location: showthread_post_start Title: galleryimagedisplay Execution Order: 5 PHP Code: Code:
$result = $db->query_first("SELECT * FROM dbtech_gallery_images WHERE roll_id = '$threadid'"); $id = $result[imageid]; $title = $result[title]; $templater = vB_Template::create('test'); $templater->register('id', $id); $templater->register('title', $title); $templatevalues['my_insertvar'] = $templater->render(); vB_Template::preRegister('SHOWTHREAD', $templatevalues); Code:
{vb:raw id} {vb:raw title} |
#244
|
|||
|
|||
I'm trying to use several of my user custom fields on a page in profile.php. I've got the template working and the page is displaying ok but I can't get the custom fields to display. I've tried a ton of stuff and just can't get it to work - would GREATLY appreciate any help you could provide. Here's what I'm doing now. This is the plug in code going into profile.php:
if ($_REQUEST['do'] == 'showsubscription') { echo 'test'; $customfields = array(); fetch_profilefields(0); $page_templater = vB_Template::create('showsubscription'); $page_templater->register('customfields', $customfields); } in the template itself I'm putting in: {vb:raw customfields.value11} where I want to use the field. I've also tried customfields.field11 various permutation of userinfo, userfield, etc. I was able to do this in vb3 no problem but having a tough time getting it to work in vB4. |
#245
|
||||
|
||||
Do you have the working 3.x code?
|
#246
|
|||
|
|||
Yes, in VB3 it was very easy - all I had to do was put $bbuserinfo[field11] on the template. The plugin portion of it was:
// ############################ start show subscription info ########################## if ($_REQUEST['do'] == 'showsubscription') { construct_usercp_nav('showsubscription'); $navbits[''] = 'Subscription Status'; $templatename = 'showsubscription'; $url =& $vbulletin->url; } |
#247
|
||||
|
||||
I'm sorry, either I don't understand what you are trying to do or are missing something big.
|
#248
|
|||
|
|||
I have a number of custom user fields created and I want for the values of those fields to appear on a profile page. In this case it's a subscription status page that lets the user know when their subscription expires and how many issues they have left remaining on their subscription. In VB3 the profile.php page had $bbuserinfo loaded with those fields and they could be called simply by putting $bbuserinfo[field11] in the template - it would put the value there.
showsubscription was the name of the custom template that I created and it would appear along with all of the profile page navigation around it. I have the showsubscription template appearing in vb4 now but I can't get the values of the custom fields to appear on the page. I'm pretty sure it will involve the {vb raw} function and registering a variable for it to be able to appear in the template but just can't figure it out. profile.php must already be loading the custom user fields somewhere because it's used elsewhere on the page. |
#249
|
|||
|
|||
I have this little random banner code I've always used through the plugin system, but I can't seem to get it to work on vb4. Can anyone help?
normally, I call for it below the navbar with this... Code:
<br> <div style="text-align: center;"> $randombanner </div> Code:
$path = '/forum/images/adbanners/'; $banners = array( array( 'src' => 'imagine.jpg', 'href' => 'http://www.talkglass.com/'), array( 'src' => 'waiting.jpg', 'href' => 'http://www.talkglass.com/), ); $rnd = rand(0,count($banners)-1); // Pick a random array index. They start with 0, so you have to -1. $href = $banners[$rnd]['href']; // Link HREF $src = $path.$banners[$rnd]['src']; // Image source $randombanner = '<a href="'.$href.'"><img border="0" src="'.$src.'" /></a>'; |
#250
|
|||
|
|||
Quote:
Try using hook parse_templates and at the end of your plugin code add: Code:
vB_Template::preRegister('navbar', array('randombanner' => $randombanner)); Then in the navbar template, use this: Code:
<br> <div style="text-align: center;"> {vb:raw randombanner} </div> |
#251
|
|||
|
|||
Thanks, Kevin. I really appreciate you taking the time to reply.
It seems I have everything right but it's not showing up. I'm not sure what else to do but this is the last thing I need before I can go live with my upgrades. VB is no help because it's custom code. I'm not sure what else to tell you that would be helpful? Would you be willing to hop over to my test forum and have a look? If so send me a pm and I'll give you login info. |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|