MyBB Community Forums

Full Version: Methods for replacing templates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I already know a few methods for replacing templates (by looking at other plugins), but I would like to know what other methods are available and which is the most efficient and the main differences between them. To list a few:

find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'returntotop\']}').'#', '', 0);

$templates->cache['forumdisplay'] = str_replace('{$footer}','{$footer}{$dynamictotop}',$templates->cache['forumdisplay']);
I use;
For inserting anything in template;
find_replace_templatesets("TEMPLATE_NAME", "#".preg_quote('THINGY-1')."#i", 'THINGY-1-THINGY-2');

For Removing the Inserting thingy;
	find_replace_templatesets("TEMPLATE_NAME", "#".preg_quote('THINGY-2')."#i", '', 0);
find_replace_templatesets() is the most common used. I have never seen the second method you described and I will have to look in the code to see what and where they are doing that and if it would work every time. But I think the editing of cache will have to be done every time the page loads. find_replace_templatesets() is more permanent.

What I sometimes also do is hook into a specific hook, see what variable are available to me that are being used in a template AFTER the hook and adding my own things to it. headerinclude for instance. I just change the $headerinclude variable to add extra javascript/css/... tags to the header-tag, instead of changing the headerinclude template.
I too use find_replace_templatesets() whenever replacing templates, but also add code to the end of variables like $headerinclude if I just want to put stuff on the end.
(2011-04-26, 05:32 PM)euantor Wrote: [ -> ]I too use find_replace_templatesets() whenever replacing templates, but also add code to the end of variables like $headerinclude if I just want to put stuff on the end.

You add code to the end of a variable? Im not quite sure what you mean ? Give us an example.
$headerinclude .= "<script src="blablabla" type="text/jacvascript" />";

Smile I probably didn't use the correct terminology for what I mean, but w/e haha
(2011-04-26, 05:08 PM)Aries-Belgium Wrote: [ -> ]I have never seen the second method you described and I will have to look in the code to see what and where they are doing that and if it would work every time. But I think the editing of cache will have to be done every time the page loads.

Yes, that has to be run every time the page loads, but it has the nice feature of not being a permanent template edit too. Which means if you add another theme, you don't need to mess with the plugin. Still, kind of defeats the purpose of the cache by adding extra processing. There are arguments for both methods.
(2011-04-26, 06:32 PM)Dylan M. Wrote: [ -> ]
(2011-04-26, 05:08 PM)Aries-Belgium Wrote: [ -> ]I have never seen the second method you described and I will have to look in the code to see what and where they are doing that and if it would work every time. But I think the editing of cache will have to be done every time the page loads.

Yes, that has to be run every time the page loads, but it has the nice feature of not being a permanent template edit too. Which means if you add another theme, you don't need to mess with the plugin. Still, kind of defeats the purpose of the cache by adding extra processing. There are arguments for both methods.

but if the function that appends the additional code has to be run regardless, then its not an issue of extra processing. but it also applies to all instances of that template if you edit the cache.

for example, the postbit template. If the item is user or post specific, then editing the cache is not correct. You are better off modifying the component variables on the fly, similar to the headerinclude edit above.