Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
[vBulletin|PHP|HTML]Automatic Template Edits
Ziki
Join Date: Nov 2005
Posts: 2,704

It appears that I am still alive

by Ziki Ziki is offline 28 Aug 2006

You are creating a new vBulletin product and want to make it more user friendly so that the users don't have to edit the templates manually?Then this tutorial will point you the right way.Basically the code I am going to demonstrate simply finds a specific part of a template and inserts our code/template into it.

Ok let's say you have a template or a piece of html code you want to include somewhere:


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

Ok now we have to options:

1. Insert the code into a template and then insert the whole template into the site.


So you now have the html code in your template called TEMPLATE.I suggest you cache it for faster loading.To do that create a new plugin on the cache_templates hook with this content:


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

$globaltemplates variable are the templates that are supposed to be cached.It is an array because it contains multiple templates,default ones and the ones from other 3rd party products.

So now what we just need to do,is insert our template into another one.To do that you need to know what page you want to display it on,the hooks that are located on that page and the templates called.In the template you will add the code in find a block of code that is static,thus won't ever be changed (like html comments).Should be one liner.Our template is TEMPLATE and the one that will include it is MAINTEMPLATE.

Then use this phpcode.


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

Explanation: the template MAINTEMPLATE is represented by the variable $vbulletin->templatecache['MAINTEMPLATE'].So we changed the way it is represented.We used str_replace to change the MAINTEMPLATE,so now it added the TEMPLATE into it.It searched for $variable and attached the template into it.

To include a code on every page use the hook global_start and the template header,footer or headinclude

DONE




2. Insert the code into a variable and insert it via replace.


Ok this is a bit easier than the code above.So we still want to add the code but don't want a template?Make the code into a variable.


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

we used ' apostrophes instead of " so that the code isn't parsed directly in the variable but rather in the template.Note,if you have ' or " in your code,you have to put \ before them so that php doesn't think it belongs into the main code.

Then just use the code before.



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

DONE


3. Insert the code into a variable and call the variable in the template.


This is the easiest way.So we have $template already specified and we just put the variable ($template) into any template and vBulletin will parse it .But you have to make sure that the hook the variable is on,is called on that page!
DONE


4. Attach code to an existing variable in the template.

This method became easier when vBulletin introduced template hooks. For instance,open template navbar and you can find $template_hook['navbar_buttons_left'] somewhere in the code,thanks to which you can add new navbar buttons simply using plugins.To do that,I make a new plugin on global_setup_complete (or any plugin called where the variable is) and add this code:


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

See the ".=" ?That means that the code following it was added to the variable's value and thus displayed when the variable is parsed .

You can also attach a whole template this way.In the next example I added my template below navbar.


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

DONE

I hope this article has helped you understand this topic and develop better and more user friendly products!

Last edited by Ziki; 16 May 2009 at 07:13..
Views: 8300
Reply With Quote
Comments
  #2  
Old 29 Aug 2006, 05:10
Basboss Basboss is offline
 
Join Date: Aug 2005
A great one, thanks a lot.
Reply With Quote
  #3  
Old 29 Aug 2006, 08:57
Ziki's Avatar
Ziki Ziki is offline
 
Join Date: Nov 2005
Thanx.

I though no one will understand what I was writing XD
__________________
My free mods~click here
Reply With Quote
  #4  
Old 01 Sep 2006, 21:53
luroca luroca is offline
 
Join Date: Jul 2002
Hello, thanks for this tutorial.

If I want to work with another page as showthread, can we use global_start and another template or I must use other hook?

Greetings
Reply With Quote
  #5  
Old 02 Sep 2006, 07:26
Ziki's Avatar
Ziki Ziki is offline
 
Join Date: Nov 2005
Use global_start and the showthread template.Or you can use any hook on showthread_page
__________________
My free mods~click here
Reply With Quote
  #6  
Old 02 Sep 2006, 13:16
luroca luroca is offline
 
Join Date: Jul 2002
Thanks again.
Reply With Quote
  #7  
Old 04 Oct 2006, 19:25
sebbe's Avatar
sebbe sebbe is offline
 
Join Date: Feb 2006
Real name: Sebastian
Thanks, will be usefull!
__________________
...
Reply With Quote
  #8  
Old 04 Oct 2006, 19:47
Zachery's Avatar
Zachery Zachery is offline
 
Join Date: Jul 2002
Real name: Zachery Woods
Bad, bad, bad, bad, bad!

You should not be adding templates or html dynamicly to pages, it only creates more hassles when users need to troubleshoot issues. Make them do the template edits or things only get worse.
__________________
Looking for ImpEx?
Reply With Quote
  #9  
Old 12 Oct 2006, 11:49
vietkieu_cz vietkieu_cz is offline
 
Join Date: Dec 2005

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

Firstly take your code and put it into a template
So I still have to edit my template to put it in?
Reply With Quote
  #10  
Old 12 Oct 2006, 19:25
Ziki's Avatar
Ziki Ziki is offline
 
Join Date: Nov 2005
You should not be adding templates or html dynamicly to pages, it only creates more hassles when users need to troubleshoot issues. Make them do the template edits or things only get worse.
??????

So I still have to edit my template to put it in?
No.Read it
__________________
My free mods~click here
Reply With Quote
  #11  
Old 12 Oct 2006, 20:48
Zachery's Avatar
Zachery Zachery is offline
 
Join Date: Jul 2002
Real name: Zachery Woods
Making your end users do less and less creates the problems we have now. Make your users do the template edits.
__________________
Looking for ImpEx?
Reply With Quote
  #12  
Old 02 Nov 2006, 15:13
Luggruff's Avatar
Luggruff Luggruff is offline
 
Join Date: May 2005
Originally Posted by Ziki
??????
As Zachery said:

I for one have a hack (CYB - Forum Rules) and he has done this, He have placed the link to the forumrules in my navbar, and I can't change it, or the location of it.

Think that's fun when you want to make a custom style? For one: If I change my navbar to the header, that link does not follow.. and so on.

Bad, bad, bad bad bad bad bad bad.
__________________
In need of a forumspace? Click here!
Reply With Quote
  #13  
Old 02 Nov 2006, 18:40
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Real name: Paul M
Whether you like automatic editing of templates is a personal choice, not a fact. If you want to discuss the merits of doing it please start a new thread, not clutter up this one.
__________________
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
  #14  
Old 03 Nov 2006, 18:35
Ziki's Avatar
Ziki Ziki is offline
 
Join Date: Nov 2005
Originally Posted by Paul M
Whether you like automatic editing of templates is a personal choice, not a fact. If you want to discuss the merits of doing it please start a new thread, not clutter up this one.
That was a plus for me?
__________________
My free mods~click here
Reply With Quote
  #15  
Old 11 Nov 2006, 20:50
RedTyger RedTyger is offline
 
Join Date: Nov 2006
Nicely done Ziki. And as an extra bonus, something I just found out. Namely that you don't need to create or insert a template if all you want to insert is one value from your settings.


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

Because str_replace uses up a little bit of resources, if whether or not it's going to be used is dependent on settings in your plugin then it's worth surrounding the replace code with a variable to ensure it only runs when it has to. Like so:


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

Reply With Quote
Reply

Similar Article
Article Author Type Replies Last Post
Wide image automatic resize for html and vB tags in posts - fixes template problems Steve St.Lauren vBulletin 3.0 Full Releases 187 10 Mar 2008 11:16



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 16:11.

Layout Options | Width: Wide Color: