PDA

View Full Version : Administrative and Maintenance Tools - Edit Templates On Fly - A step towards easy-to-install modifications


Milad
04-23-2007, 10:00 PM
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:
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.
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 ./
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 (http://us.php.net/ereg_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 (http://us.php.net/preg_replace) (or ereg_replace (http://us.php.net/ereg_replace), respectively) to help simplify the joys of Regex (http://en.wikipedia.org/wiki/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
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
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 (http://php.net/preg_replace) in order to make on-the-fly template changes to all templates -- including modified ones:

$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:
<div>

And, in the users template it's changed to:
<div class="alt1">

Your str_replace is NOT going to find it:
$output = str_replace('<div>', '<div id="MyID">', $text);

However, using preg_replace WILL:
$output = preg_replace('/<div(.*)>/', '<div id="MyID">', $text);

In my example code above, I use preg_match (http://php.net/preg_match) to retain the user's variables even after inserting my own:
preg_match('/<div(.*)>/', $text, $match);
$output = preg_replace('/<div(.*)>/', '<div' . $match[1] . ' id=\"MyID\">', $text);

ragtek
04-24-2007, 02:51 PM
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
Here's an example from one of my modifications using preg_replace (http://php.net/preg_replace) in order to make on-the-fly template changes to all templates -- including modified ones:

$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:
<div>

And, in the users template it's changed to:
<div class="alt1">

Your str_replace is NOT going to find it:
$output = str_replace('<div>', '<div id="MyID">', $text);

However, using preg_replace WILL:
$output = preg_replace('/<div(.*)>/', '<div id="MyID">', $text);

In my example code above, I use preg_match (http://php.net/preg_match) to retain the user's variables even after inserting my own:
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.