Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles
FAQ Community Calendar Today's Posts Search

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
  #42  
Old 08-04-2009, 01:02 PM
Omar Al-Ansari Omar Al-Ansari is offline
 
Join Date: Jun 2009
Posts: 41
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi all,

I have tried this and it works good however it only works within vb if I try to use the same template in vba it does not?

any help will be appreciated

Product: vBulletin
Hook Location: global_start

Code:
eval('$adv_portal_rotating_articles = "' . fetch_template('adv_portal_rotating_articles') . '";');
Reply With Quote
  #43  
Old 08-04-2009, 01:04 PM
ragtek ragtek is offline
 
Join Date: Mar 2006
Location: austria, croatia
Posts: 1,630
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

global_start is too early
try global_setup_complete instead
Reply With Quote
  #44  
Old 08-04-2009, 05:34 PM
Omar Al-Ansari Omar Al-Ansari is offline
 
Join Date: Jun 2009
Posts: 41
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by ragtek View Post
global_start is too early
try global_setup_complete instead
Hey ragtek,

Still not working

if i use this $adv_portal_rotating_articles in forumhome or any vb page it works fine

but not in vba

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

Quote:
Originally Posted by Omar Al-Ansari View Post
Hey ragtek,

Still not working

if i use this $adv_portal_rotating_articles in forumhome or any vb page it works fine

but not in vba
ok nevermind I got it working ..

it should be defined through CMPS CP
Reply With Quote
  #45  
Old 08-04-2009, 05:52 PM
ragtek ragtek is offline
 
Join Date: Mar 2006
Location: austria, croatia
Posts: 1,630
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

or also $GLOBALS[adv_portal_rotating_articles] should also work
Reply With Quote
  #46  
Old 10-12-2009, 03:16 AM
Dax IX Dax IX is offline
 
Join Date: Jul 2005
Posts: 153
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by iVox View Post
Compliments to Kerry-Anne for posting this.

I'm new to vB, but a long time PHP guru and forum admin (including uBB and phpBB).

After reviewing some of the replies here I wish to add that the information presented here is not simply copy/paste, and requires basic knowledge of PHP as well as the hook and template system of vB. I noticed some of the more experienced PHP developers making suggestings and fixes here. I concur and wish to expand on that.

I tried the procedure, and with a few minor corrections got it to work with less overhead simply by optimizing the commands (using advice posted earlier plus new advice from me):

I can sum it up in 4 easy steps (vb 3.6.7+):

1) Create a custom template, i.e. "mytemplate" for purposes of these instructions

Put in there "Hello World!" or any HTML so you know its working when it displays.

2) Add a new plugin, suggested name is "mytemplate_plugin" for easy reference

Pick hook as "global_start" if you want your template available in any other, or select the appropriate hook depending on where you want your template to display. The latter is the better choice if you are customizing a specific feature of vB.

Set plugin PHP code: $mytemplate_plugin = fetch_template('mytemplate_plugin_home');

3) Create another plugin, suggested name is: "Cached - mytemplate_plugin"

Pick hook as "cache_template" (located within vBulletin; General hooks in pulldown)

Set plugin PHP code: $globaltemplates[]='mytemplate_plugin';

4) In whatever template you wish (based on step 2) place the variable $mytemplate_plugin where you want your custom template to display. If successful, you'll see your "Hello World!" or your HTML as in step 1.

Folks, that's all there is to it. Notice no need for eval or array_merge.

This approach follows a simple naming convention that's easy to understand long after you implement and forget, and uses the most efficient PHP to get the job done, and will work perfectly without have to hack any native vB code. Yes, there are a million ways to skin a cat, this is one, but if you start doing alot of customization of this type, keeping the naming convention and optimizing the PHP code will save valuable server resources.

Compliments to all who posted.

Thanks for your time and if I missed something, please let me know.

-jim
I'm a little confused. The code in this post makes very little sense to me. Is it still valid?

Quote:
Originally Posted by IdanB View Post
2 plugins:
1] location: "global_start"
name: "alt_lp_template"
with followign code:
PHP Code:
$alt_lp_template fetch_template('alternate_view'); 
2] location: "cache_templates"
name: "Tempalte Cache"
with followign code:
PHP Code:
$globaltemplates[] = 'alt_lp_template'
and on forum i keep getting "uncached template" warning for "alternate_view".
Note this template was added to master style.

EDIT: what's more weird, i know it's cached, as later on i can access it from $vbulletin->templatecache['alternate_view']
Quote:
Originally Posted by ragtek View Post
You have to make it this way:
to cache it:
PHP Code:
 $globaltemplates[] = 'alternate_view'
and save the content from the template into a variable:
PHP Code:
eval('$varname = "' fetch_template('alternate_view') . '";'); 
Now you can use $varname
Quote:
Originally Posted by IdanB View Post
thanks, that solved it, wrote under cache name the var name instead... silly me
Can you explain exactly what you did to fix this? Did you still need the eval() code?

I can get the contents of my test template to show, but it escapes my quotes within my HTML, so no styling is applied.

--------------- Added 12 Oct 2009 at 12:11 ---------------

Here's the code that I used:

2 plugins:
1] location: "global_start"
name: "test_template_plugin"
with followign code:
PHP Code:
$test_template fetch_template('test_template'); 
2] location: "cache_templates"
name: "Cached - test_template"
with followign code:
PHP Code:
$globaltemplates[]='test_template'
Like I said, I can see my content, but quotes are escaped, so no styling is applied.

my template contents (test_template) (VERY simple)

HTML Code:
<table class="test_template_table">
    <tr>
        <td class="test_template_td">
            Hi there!
        </td>
    </tr>
</table>
What am I doing wrong?

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

I was trying to follow the advice of the first post that I quoted, and it just didn't work.

I changed the first plugin to include the eval() code for my variable, and it worked properly.

Sorry for the bother.
Reply With Quote
  #47  
Old 12-15-2010, 06:52 PM
Assopoker Assopoker is offline
 
Join Date: Dec 2007
Posts: 15
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This is a best way to add a template to a product in vb4?
Reply With Quote
  #48  
Old 07-08-2011, 01:38 PM
EquinoxWorld EquinoxWorld is offline
 
Join Date: Nov 2009
Location: Naples
Posts: 354
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Also would like to know if this article is still valid? Anyone can provide us with some confirmation please.
Reply With Quote
  #49  
Old 07-08-2011, 02:43 PM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No this is only valid for the 3.x series of vBulletin. Check the VB 4.x Articles section for more relevant articles.
Reply With Quote
  #50  
Old 10-27-2011, 09:44 AM
TTayfun's Avatar
TTayfun TTayfun is offline
 
Join Date: Aug 2011
Location: Istanbul, Turkey
Posts: 211
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

but why not working in STANDARD_REDIRECT template ? how can i do?
Reply With Quote
  #51  
Old 08-07-2012, 07:38 PM
BlueChipEarth's Avatar
BlueChipEarth BlueChipEarth is offline
 
Join Date: Apr 2009
Posts: 21
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I have tried everything you have provided to the very letter, and it simply doesn't work at all... the variable is always null when I call for it in the template.

I'm trying to add a custom template to the forumhome_forumbit_level1_post template.

What am I doing wrong!?

Here's how I have it set up:

New Custom Template - title is: sponsorForumButtons

Right now I just have it filled with "TESTING THE NEW TEMPLATE"

I have added a new Plugin, which is active, in global_start (I've tried in forumdisplay_complete, forumdisplay_start, global_complete, all the forumhome hooks... I've tried every single hook location I could think of... none seem to work)

This is the exact code of the Plugin:

//Sponsor Forum Buttons

eval('$sponsorForumButtons = "' . fetch_template('sponsorForumButtons') . '";');

Then in the forumhome_forumbit_level1_post template I have:

$sponsorForumButtons

Right where I want it to show up.

No matter what I try the variable just doesn't show up... even if I EXPLICITLY set the variable in the forumdisplay_start (or any other) hook... shouldn't the global_complete hook be available to every template!? Even so, how is it that the variable is ALWAYS null? It seems like the variable will show up in the FORUMDISPLAY template, but if I put it into the forumhome_forumbit_level1_post template, the variable is inexplicably null.

I have even tried to cache the template in cache_templates and parse_templates. That code looks like this:

$globaltemplates[]='sponsorForumButtons';

NOTHING WORKS!

I am having a nervous breakdown. Please save me from killing myself...
Reply With Quote
Reply


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 07:59 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.04933 seconds
  • Memory Usage 2,331KB
  • Queries Executed 25 (?)
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
  • (3)bbcode_code
  • (1)bbcode_html
  • (6)bbcode_php
  • (6)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
  • (11)post_thanks_box
  • (4)post_thanks_box_bit
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)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