vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3.6 Add-ons (https://vborg.vbsupport.ru/forumdisplay.php?f=194)
-   -   Administrative and Maintenance Tools - Edit Templates On Fly - A step towards easy-to-install modifications (https://vborg.vbsupport.ru/showthread.php?t=145649)

Milad 04-23-2007 10:00 PM

Edit Templates On Fly - A step towards easy-to-install modifications
 
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.

nexialys 04-24-2007 10:26 AM

good idea for new functions...

actually you do not need to check function_exists ... there is no such functions in vBulletin anyway...

valdet 04-24-2007 10:30 AM

Installed, waiting for user feedback..

magnus 04-24-2007 11:25 AM

Isn't this just basically renaming str_replace? From a deployment standpoint, I'm not sure a prettier function is worth the cost of a dependency. There's really no actual gain.

However, if you were to create a function that utilizies preg_replace (or ereg_replace, respectively) to help simplify the joys of Regex for some of the greener developers, that would probably be more well adopted.

I'm not knocking it, I think it's great that someone is taking an initiative. I'm just thinking down the road, as a developer, of possible support quandries that could arise when having to explain to end users about dependencies, etc.

ZomgStuff 04-24-2007 11:25 AM

Will try when I get home.

ragtek 04-24-2007 11:41 AM

yes, ist nothing other then str_replace
heres i much better method http://www.vbhacks-germany.org/showthread.php?t=5572

nexialys 04-24-2007 12:06 PM

Quote:

Originally Posted by ragtek (Post 1234588)
yes, ist nothing other then str_replace
heres i much better method http://www.vbhacks-germany.org/showthread.php?t=5572

please provide the method, not a link on a site that requires registration...

oh, and btw, this is not a Mod or a Addon... it's just some functions that would help coders in some situations... please DO NOT INSTALL if you don't know what to do with this...

Bolas 04-24-2007 12:36 PM

Quote:

Originally Posted by nexialys (Post 1234603)
please provide the method, not a link on a site that requires registration...

oh, and btw, this is not a Mod or a Addon... it's just some functions that would help coders in some situations... please DO NOT INSTALL if you don't know what to do with this...

Does vb.org need a section <em>ad hoc</em> for user function or libraries..?

magnus 04-24-2007 12:47 PM

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); 


ragtek 04-24-2007 02:51 PM

Quote:

Originally Posted by nexialys (Post 1234603)
please provide the method, not a link on a site that requires registration...

oh, and btw, this is not a Mod or a Addon... it's just some functions that would help coders in some situations... please DO NOT INSTALL if you don't know what to do with this...

it changes direkt the template on hack install or you change it if it isnt available
its not on the fly, it changes on the hack install, or on your change(you have a field template change in de acp)
there you can set a search string and a change string....
like this method here, but not on fly...
sorry for the bad english, i hope i helped

Milad 04-26-2007 12:23 PM

Quote:

Originally Posted by magnus (Post 1234621)
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.


All times are GMT. The time now is 07:14 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.01257 seconds
  • Memory Usage 1,835KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_html_printable
  • (11)bbcode_php_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (11)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete