Go Back   vb.org Archive > vBulletin Modifications > Archive > vB.org Archives > vBulletin 3.6 > vBulletin 3.6 Add-ons
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Edit Templates On Fly - A step towards easy-to-install modifications Details »»
Edit Templates On Fly - A step towards easy-to-install modifications
Version: 1.0.0, by Milad Milad is offline
Developer Last Online: Nov 2023 Show Printable Version Email this Page

Category: Administrative and Maintenance Tools - Version: 3.6.5 Rating:
Released: 04-23-2007 Last Update: Never Installs: 11
Uses Plugins
 
No support by the author.

Description:
This will enable modifications writers to edit templates on fly, to make there modifications as easy-to-install as possible. It consists of some functions, which had been written to ease template edits, without any actual editing from the webmasters side,

Installation:
Just import the XML file as a product.
Click install to receive updates and support.

Documentation:
All functions are declared in the hook init_startup, with Execution Order of 1, to be the first plug-in which is being executed in the whole scripts.

Current version contains the following functions:
  • insert_to_template

    Inserts something to a template.

    • return: nothing

    void insert_to_template (string $templatename, string $location, string $addition, [mixed $position = 'after'])

    • string $templatename: The template name to modify (ex: header, footer etc.).
    • string $location: A unique text in the template, where you want to insert your addition. If this text is not unique then the addition will be joined to all instances.
    • string $addition: The addition.
    • mixed $position: has two possible values, after (The default one): to insert the addition after the location, before: to insert the addition before the location.

  • delete_from_template

    Deletes something from a template.

    • return: nothing

    void delete_from_template (string $templatename, string $texttodelete)

    • string $templatename: The template name to delete from (ex: header, footer etc.).
    • string $texttodelete: A unique text in the template to be deleted. If this text is not unique then all instances will be deleted.

  • replace_in_template

    Replaces a text in a template.

    • return: nothing

    void replace_in_template (string $templatename, string $texttofind, string $texttoreplace)

    • string $templatename: The template name to replace in (ex: header, footer etc.).
    • string $texttofind: A unique text in the template to replace. If this text is not unique then all instances will be replaced.
    • string $texttoreplace: The replacing text.

  • print_template

    Print a template in the browser, this helps you to see the working content of a template which is different from what do you see in the templates editor, so you can choose the proper text from a template.

    • return: nothing, but it prints the template in the browser

    void print_template (string $templatename)

    • string $templatename: The template name to print in the browser (ex: header, footer etc.).


Restrictions:
  • You can't modify a template after it had been fetched by fetch_template function.
  • Because no template is cached before the hook global_start, using those functions in following hooks makes nonsense:
    • init_startup
    • style_fetch
    • cache_templates


Examples:
  • Add an image to the header:
    PHP Code:
    replace_in_template('header''&nbsp;''<img src="bla bla bla" alt="" border="0" />'); 
  • Insert a template inside another template, this will reduce the number of eval()s leading to better performance.
    PHP Code:
    insert_to_template('FORUMHOME''<!-- logged-in users -->'$vbulletin->templatecache['sometemplate'], 'before'); 
    Of course the template (sometemplate) must be already cashed.

  • Replace the index.php in the header with ./
    PHP Code:
    replace_in_template('header''href=\"" . $GLOBALS[\'vbulletin\']->options[\'forumhome\'] . ".php" . $GLOBALS[\'vbulletin\']->session->vars[\'sessionurl_q\'] . "\"''href=\"./" . $GLOBALS[\'vbulletin\']->session->vars[\'sessionurl_q\'] . "\"'); 

Notes for coders:
Feel free to use those functions in your mods, and please don't forget to add a dependency for this product (edit_templates_onfly). This will guarantee that every user of your mod will install this modification, so your mod will function properly, this mod will be a centralized method to edit templates, a method which is subject to be developed well, so you can't copy those functions in your mods.
Your suggestions and participations are welcomed.

Change log:
  • April, 19 2007, Initial release 1.0.0, functions: insert_to_template, delete_from_template, replace_in_template and print_template.

Credits:
The first person whom I saw uses this method to edit templates is Zero Tolerance in his vBShout mod, I learned it there.

Show Your Support

  • This modification may not be copied, reproduced or published elsewhere without author's permission.

Comments
  #12  
Old 04-26-2007, 12:23 PM
Milad's Avatar
Milad Milad is offline
 
Join Date: Apr 2005
Location: Syro
Posts: 663
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by magnus View Post
Here's an example from one of my modifications using preg_replace in order to make on-the-fly template changes to all templates -- including modified ones:

PHP Code:
$find '/<tr(.*)>/';
preg_match($find$vbulletin->templatecache['threadbit'], $match);
$vbulletin->templatecache['threadbit'] = preg_replace($find'<tr' $match[1] . ' id=\"vbpostrow_$thread[' . ($vbulletin->options['ajax_firstpost_firstlast'] ? 'lastpostid' 'firstpostid') . ']\">'$vbulletin->templatecache['threadbit']);
unset(
$find$match);
            
$find '/\$thread\[title_editable\](\s*)<div(.*)?>/';
preg_match($find$vbulletin->templatecache['threadbit'], $match);
$vbulletin->templatecache['threadbit'] = preg_replace($find$match[0] . $match[1] . '    <img id=\"vbpostimg_$thread[' . ($vbulletin->options['ajax_firstpost_firstlast'] ? 'lastpostid' 'firstpostid') . ']\" src=\"$stylevar[imgdir_button]/expand.gif\" onclick=\"return vbpost_get($thread[' . ($vbulletin->options['ajax_firstpost_firstlast'] ? 'lastpostid' 'firstpostid') . '])\" onMouseOver=\"this.style.cursor=\'pointer\';\" />'$vbulletin->templatecache['threadbit']);
unset(
$find$match); 
The problem with using str_replace is that most people have a tendency to edit their templates. Thus, the static text you're searching the template for may have been changed. Now, if the text has been removed altogether there's nothing you can do -- however, if say by default you're looking for:
HTML Code:
<div>
And, in the users template it's changed to:
HTML Code:
<div class="alt1">
Your str_replace is NOT going to find it:
PHP Code:
$output str_replace('<div>''<div id="MyID">'$text); 
However, using preg_replace WILL:
PHP Code:
$output preg_replace('/<div(.*)>/''<div id="MyID">'$text); 
In my example code above, I use preg_match to retain the user's variables even after inserting my own:
PHP Code:
preg_match('/<div(.*)>/'$text$match);
$output preg_replace('/<div(.*)>/''<div' $match[1] . ' id=\"MyID\">'$text); 

You presented a very good points, I'll implement them for sure.
I hadn't thought about preg_replace ereg_replace and preg_match, they must be used.

Thank you very much.
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 04:35 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.09266 seconds
  • Memory Usage 2,258KB
  • Queries Executed 18 (?)
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
  • (2)bbcode_html
  • (7)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_post
  • (1)navbar
  • (6)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (2)post_thanks_box
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit_info
  • (1)postbit
  • (2)postbit_onlinestatus
  • (2)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_postinfo_query
  • fetch_postinfo
  • 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