vBulletin Mods

The Official vBulletin Modifications Site
https://www.vbulletin.org/forum/showthread.php?t=119933

How To Include A Custom Template Via Plugins
by peterska2
29 Jun 2006 23:42

This tutorial will show you how to include custom templates within your vBulletin pages using plugins.

First of all, you need to create your custom template. Lets call this mytemplate for the sake of this tutorial.

Once you have created your template, and decided where you would like to have it placed, you can put the template variable $mytemplate there.

As vBulletin does not yet know what to do with this template variable, it simply ignores it when generating the page.

Next you need to create the plugins for the template, so that vBulletin knows what to do with it. You need to create two plugins, one to include the template, and one to cache it (saves a query).

The easiest way to do this, although not technically the best, is to create the first plugin in either the hook location global_start or parse_templates This allows the template to be used globally throughout your site. However, you can also create this plugin in the _complete hook for the location that you wish to have it displayed (eg postbit_complete memberinfo_complete etc).

The content of this first plugin will be as follows:
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This tells it that when it comes across $mytemplate, it is to fetch the template mytemplate and include it there. This plugin can be named anything that you want, and should be set to active.

The second plugin is always placed in the hook location cache_templates as this is the one that literally does what it says on the tin. It caches the template, preventing an extra query on each page that it is included on.

The content of this plugin will be as follows:
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This tells it to add mytemplate to the global templates array, which ensures that everywhere that it is used, it is cached. Again, this plugin can be named anything that you want, but not the same as the first one, and should be set to active.


Congratulations, your custom template is now included on your site without the need for any code modifications, and is already up and running.

If at any point you decide that you want to remove the custom template, either temporarily or permanently, you can simply disable the two plugins and it will again vanish.


My personal preference is to use the parse_templates hook for the first plugin as this enables the template to be used on every page on your site so you can move it about without having to edit the plugin. It is also useful if you wish to have it included on two or more pages.


The method explained in this tutorial is the exact same method as I have used for the following modifications:

Aurons Ghost 20 Jul 2006 09:15

Thank you for this. I've been trying to split up some templates I've changed quite a lot into some easier chunks and wasn't sure how before I read this :)

One question though, can I use the same plugin to parse multiple templates or do they have to be individual?

eg,
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

or is that impossible / unwise?

peterska2 20 Jul 2006 10:18

Yes that will work, however for the second one
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

is better as otheriwse you are merging the array twice where you don't need to.

Aurons Ghost 20 Jul 2006 14:25

ah, thank you :) I hadn't realised that.

PennylessZ28 21 Jul 2006 20:38

Question what if I wanted to replace a template al together?

Lets say, instead of calling forum_home, i want a plugin that will call forum_new instead ??

peterska2 21 Jul 2006 21:43

You would need to put a conditional in your forumhome template so that if the plugin was enabled then it would use the forum_new, and if it wasn't, or some other condition, then it would use forum_home

Aurons Ghost 23 Jul 2006 22:22

I've been trying to add a template in part of the forumhome_forumbit_x templates and it's not working. I've followed the instructions above (adding the parse plugin to parse_templates). I've also tried it out by adding it to another template such as the footer and it appears. Are there some templates that can't have another template added inside them?

Paul M 23 Jul 2006 22:59

You shouldn't be merging arrays for this, that's very wasteful of resources, just add to the array ;


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


Renmiri 24 Jul 2006 01:47

Quote:

Originally Posted by Paul M
You shouldn't be merging arrays for this, that's very wasteful of resources, just add to the array ;


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

.= instead of = right ?

Kaitlyn2004 01 Aug 2006 06:50

Quote:

Originally Posted by Renmiri
.= instead of = right ?

ummm, no?

[] adds to the end of the array

Cheez-It 25 Sep 2006 17:37

Do you need the first plugin as well when using that code Paul?

I'm actually trying to do this...

My goal is to have a template custom_header with a variable $custom_header which will contain all of the code from $header with a slight change (removing code for a right column and a different logo) for a CMPS home page...

1.) I do add new template, and put the code I want in it, saving it as custom_header. It now appears under "Custom Templates"

2.) I make a plugin with
Quote:

eval('$custom_header = "' . fetch_template('custom_header') . '";');
starting at global_start

3.) I add the plugin "globaltemplates[] = 'custom_header';" starting in cache template

I enable both.

I get error:Parse error: syntax error, unexpected '[' in /home/uninsta1/public_html/bu/forums/global.php(346) : eval()'d code on line 55

Right above the logo in ALL pages (not just CMPS...)

Shortly thereafter forums don't show and errors occur in functions.php as well.

I disable plugins.

What should I try next?

bitHacker 14 Oct 2006 11:41

if you don't put $ before globaltemplates[], that won't work...

HPIA 31 Oct 2006 02:12

Oh god, this is confusing. Is there any way of explaining this to someone illiterate in the technical coding world?

I want to incorporate my custom template into the forum and $mycustomtemplate isn't working.

Edit: Sorry for this post, I am an idiot. Worked it out. GREAT tutorial, this is EXACTLY what I was looking for ^_^

XXP 23 Apr 2007 14:07

Quote:

Originally Posted by Kerry-Anne (Post 1018718)
... The easiest way to do this, although not technically the best,...

Thanks for this crucial and very helpful information. Should be in the vB manual!

A followup please: what exactly is "the technically best" way to do it?

Thanks.

puck916 26 Apr 2007 14:12

I have this working completely, except in my hook I have $myvar = "test";

Now in my header template I have :
$myvar

Nothing happens. how can I get $myvar to be replaced with the dynamically created value from the hook?


All times are GMT. The time now is 18:29.

Powered by vBulletin® Version 3.8.12
Copyright © 2019, MH Sub I, LLC dba vBulletin. All Rights Reserved. vBulletin® is a registered trademark of MH Sub I, LLC
Copyright ©2001 - , vbulletin.org. All rights reserved.