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

Reply
 
Thread Tools
[vBulletin|PHP|HTML]Automatic Template Edits
Ziki's Avatar
Ziki
Join Date: Nov 2005
Posts: 2,704

It appears that I am still alive

Show Printable Version Email this Page Subscription
Ziki Ziki is offline 08-27-2006, 10:00 PM

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:

HTML Code:
<em>My name is $name and welcome to $board</em>
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:

PHP Code:
$globaltemplates  array_merge($globaltemplates, array('TEMPLATE''TEMPLATE2')); 
$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.

PHP Code:
$variable 'OUR STATIC CODE';
$vbulletin->templatecache['MAINTEMPLATE'] = str_replace($variable,$variable.fetch_template('TEMPLATE'),$vbulletin->templatecache['MAINTEMPLATE'])
;]=]=> 
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.

PHP Code:
$template '<em>My name is $name and welcome to $board</em>'
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.


PHP Code:
$template '<em>My name is $name and welcome to $board</em>';
$variable 'OUR STATIC CODE';
$vbulletin->templatecache['MAINTEMPLATE'] = str_replace($variable,$variable.$template,$vbulletin->templatecache['MAINTEMPLATE'])
;]=]=> 
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:

PHP Code:
$template_hook['navbar_buttons_left'] .= '<td class="vbmenu_control"><a href="file.php' $session['sessionurl_q'] . '" accesskey="5" >' $vbphrase['mynewbutton'] . '</a></td>'
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.

PHP Code:
eval('$navbar .= "' fetch_template('mytemplate') . '";'); 
DONE

I hope this article has helped you understand this topic and develop better and more user friendly products!
Reply With Quote
  #2  
Old 08-29-2006, 04:10 AM
Basboss Basboss is offline
 
Join Date: Aug 2005
Posts: 36
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

A great one, thanks a lot.
Reply With Quote
  #3  
Old 08-29-2006, 07:57 AM
Ziki's Avatar
Ziki Ziki is offline
 
Join Date: Nov 2005
Posts: 2,704
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanx.

I though no one will understand what I was writing XD
Reply With Quote
  #4  
Old 09-01-2006, 08:53 PM
luroca luroca is offline
 
Join Date: Jul 2002
Posts: 142
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 09-02-2006, 06:26 AM
Ziki's Avatar
Ziki Ziki is offline
 
Join Date: Nov 2005
Posts: 2,704
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Use global_start and the showthread template.Or you can use any hook on showthread_page
Reply With Quote
  #6  
Old 09-02-2006, 12:16 PM
luroca luroca is offline
 
Join Date: Jul 2002
Posts: 142
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks again.
Reply With Quote
  #7  
Old 10-04-2006, 06:25 PM
sebbe's Avatar
sebbe sebbe is offline
 
Join Date: Feb 2006
Location: .se
Posts: 195
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks, will be usefull!
Reply With Quote
  #8  
Old 10-04-2006, 06:47 PM
Zachery's Avatar
Zachery Zachery is offline
 
Join Date: Jul 2002
Location: Ontario, Canada
Posts: 11,440
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #9  
Old 10-12-2006, 10:49 AM
vietkieu_cz vietkieu_cz is offline
 
Join Date: Dec 2005
Posts: 120
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
<templates>
        <
template name="zh_nrc" templatetype="template" date="1148052896" username="Ziki" version="3.5.4"><![CDATA[
        <
script language=JavaScript>

var 
message="$zhnrctext";
function 
clickIE() {if (document.all) {alert(message);return false;}}
function 
clickNS(e) {if 
(
document.layers||(document.getElementById&&!document.all)) {
if (
e.which==2||e.which==3) {alert(message);return false;}}}
if (
document.layers
{
document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{
document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

</
script>
]]></
template>
    </
templates
Quote:
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 10-12-2006, 06:25 PM
Ziki's Avatar
Ziki Ziki is offline
 
Join Date: Nov 2005
Posts: 2,704
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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.
??????

Quote:
So I still have to edit my template to put it in?
No.Read it
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:30 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.04727 seconds
  • Memory Usage 2,311KB
  • 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
  • (1)bbcode_html
  • (7)bbcode_php
  • (3)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
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (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
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • 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