Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
How To Include A Custom Template Via Plugins
peterska2
Join Date: Oct 2003
Posts: 6,504

Manchester, UK
by peterska2 peterska2 is offline 29 Jun 2006

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:
Views: 35870
Reply With Quote
Comments
  #2  
Old 20 Jul 2006, 09:15
Aurons Ghost Aurons Ghost is offline
 
Join Date: Jul 2006
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?
Reply With Quote
  #3  
Old 20 Jul 2006, 10:18
peterska2 peterska2 is offline
 
Join Date: Oct 2003
Real name: Kerry-Anne (really!)
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.
__________________
Kerry-Anne
Previously known as peterska2

My Sites: peterska2.co.uk Super Clickers World News Zone English Football League Popular Usenet Groups
Do NOT PM or email me for Support or Modification Requests - No Exceptions!!
I do NOT do custom requests. Do not ask for them. - No Exceptions!!
Reply With Quote
  #4  
Old 20 Jul 2006, 14:25
Aurons Ghost Aurons Ghost is offline
 
Join Date: Jul 2006
ah, thank you I hadn't realised that.
Reply With Quote
  #5  
Old 21 Jul 2006, 20:38
PennylessZ28 PennylessZ28 is offline
 
Join Date: Mar 2002
Real name: Mike
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 ??
Reply With Quote
  #6  
Old 21 Jul 2006, 21:43
peterska2 peterska2 is offline
 
Join Date: Oct 2003
Real name: Kerry-Anne (really!)
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
__________________
Kerry-Anne
Previously known as peterska2

My Sites: peterska2.co.uk Super Clickers World News Zone English Football League Popular Usenet Groups
Do NOT PM or email me for Support or Modification Requests - No Exceptions!!
I do NOT do custom requests. Do not ask for them. - No Exceptions!!
Reply With Quote
  #7  
Old 23 Jul 2006, 22:22
Aurons Ghost Aurons Ghost is offline
 
Join Date: Jul 2006
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?
Reply With Quote
  #8  
Old 23 Jul 2006, 22:59
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Real name: 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.

__________________
Former vBulletin.org Staff Member


Cable Forum
Please do not PM me about custom work - I no longer undertake any.
Note: I will not answer support questions via e-mail or PM - please use the relevant thread or forum.
Reply With Quote
  #9  
Old 24 Jul 2006, 01:47
Renmiri Renmiri is offline
 
Join Date: Jul 2006
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 ?
Reply With Quote
  #10  
Old 01 Aug 2006, 06:50
Kaitlyn2004 Kaitlyn2004 is offline
 
Join Date: Jul 2006
Originally Posted by Renmiri
.= instead of = right ?
ummm, no?

[] adds to the end of the array
Reply With Quote
  #11  
Old 25 Sep 2006, 17:37
Cheez-It Cheez-It is offline
 
Join Date: Mar 2006
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
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?

Last edited by Cheez-It; 25 Sep 2006 at 18:32.
Reply With Quote
  #12  
Old 14 Oct 2006, 11:41
bitHacker bitHacker is offline
 
Join Date: Sep 2005
if you don't put $ before globaltemplates[], that won't work...
Reply With Quote
  #13  
Old 31 Oct 2006, 02:12
HPIA HPIA is offline
 
Join Date: Jul 2006
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 ^_^

Last edited by HPIA; 31 Oct 2006 at 03:16.
Reply With Quote
  #14  
Old 23 Apr 2007, 14:07
XXP XXP is offline
 
Join Date: Jan 2007
Originally Posted by Kerry-Anne View Post
... 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.
Reply With Quote
  #15  
Old 26 Apr 2007, 14:12
puck916 puck916 is offline
 
Join Date: Jan 2007
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?
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 00:10.

Layout Options | Width: Wide Color: