Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles

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

 

Manchester, UK
Show Printable Version Email this Page Subscription
peterska2 peterska2 is offline 06-29-2006, 10:00 PM

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:
Code:
eval('$mytemplate = "' . fetch_template('mytemplate') . '";');
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:
Code:
$globaltemplates = array_merge($globaltemplates, array('mytemplate'));
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:
Reply With Quote
  #2  
Old 07-20-2006, 08:15 AM
Aurons Ghost Aurons Ghost is offline
 
Join Date: Jul 2006
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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,
Code:
eval('$mytemplate = "' . fetch_template('mytemplate') . '";');
eval('$mytemplate2 = "' . fetch_template('mytemplate2') . '";');
Code:
$globaltemplates = array_merge($globaltemplates, array('mytemplate'));
$globaltemplates = array_merge($globaltemplates, array('mytemplate2'));
or is that impossible / unwise?
Reply With Quote
  #3  
Old 07-20-2006, 09:18 AM
peterska2 peterska2 is offline
 
Join Date: Oct 2003
Location: Manchester, UK
Posts: 6,504
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yes that will work, however for the second one
Code:
$globaltemplates = array_merge($globaltemplates, array('mytemplate', 'mytemplate2'));
is better as otheriwse you are merging the array twice where you don't need to.
Reply With Quote
  #4  
Old 07-20-2006, 01:25 PM
Aurons Ghost Aurons Ghost is offline
 
Join Date: Jul 2006
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

ah, thank you I hadn't realised that.
Reply With Quote
  #5  
Old 07-21-2006, 07:38 PM
PennylessZ28 PennylessZ28 is offline
 
Join Date: Mar 2002
Location: North America
Posts: 737
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 07-21-2006, 08:43 PM
peterska2 peterska2 is offline
 
Join Date: Oct 2003
Location: Manchester, UK
Posts: 6,504
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #7  
Old 07-23-2006, 09:22 PM
Aurons Ghost Aurons Ghost is offline
 
Join Date: Jul 2006
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 07-23-2006, 09:59 PM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

PHP Code:
globaltemplates[] = 'mytemplate'
Reply With Quote
  #9  
Old 07-24-2006, 12:47 AM
Renmiri Renmiri is offline
 
Join Date: Jul 2006
Location: Chicago, USA
Posts: 140
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 ;

PHP Code:
globaltemplates[] = 'mytemplate'
.= instead of = right ?
Reply With Quote
  #10  
Old 08-01-2006, 05:50 AM
Kaitlyn2004 Kaitlyn2004 is offline
 
Join Date: Jul 2006
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Renmiri
.= instead of = right ?
ummm, no?

[] adds to the end of the array
Reply With Quote
Reply

Thread Tools

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

Forum Jump


All times are GMT. The time now is 06:29 PM.


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.14429 seconds
  • Memory Usage 2,285KB
  • Queries Executed 23 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_code
  • (2)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (4)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete