PDA

View Full Version : How-To Cache Templates


Princeton
05 Jan 2006, 15:46
This article assumes that you are building your own product with end-user options.


INTRODUCTORY ON CACHING TEMPLATES:
Whenever you create a product you should cache your templates by adding them to the $globaltemplates and $actiontemplates array.

$globaltemplates are templates loaded by all actions.
$actiontemplates are templates loaded when a specified action is called such as $do (e.g. ?do=edit).


TEMPLATES NOT CACHED:
When displaying an "error message" on the same page I notice that the templates are not cached.
This is due to the fact that the "action" does not have any templates to load (via $actiontemplates).*

To remedy this some coders add the uncached templates to the $globaltemplates array. However, this is the wrong way to do it. As a coder, our obligation is to cache the least amount of templates to consume less memory.

*NOTE: This usually happens when you are redirected back to the page via $_POST.


THE FIX:
To cache these templates, we add the following:$actiontemplates['insertsettings'] =& $actiontemplates['options'];below the $actiontemplates array.


EXAMPLE:
A blog product that I am working on will display an error message to the end-user upon an error. The error message will be on the same page (redirected back via $_POST) not a STANDARD_ERROR page.

The interface is full of options that at the very least requires the end-user to enter a TITLE and DESCRIPTION.

The interface is accessible by the action "do=options".
To cache the required templates to build the interface I add the templates to the $actiontemplates array such as:$actiontemplates = array(
'options' => array(
'gtblog_options',
'gtblog_radio_option',
'newpost_errormessage',
)
Back to the interface...
whenever the end-user forgets to enter a TITLE an error message is displayed. (The system requires the title.)

When the error message is displayed none of the templates are cached. We are missing something... ;)

To remedy this we need to look at the $_POST "do" action of the form. A closer look at the html source tells me that the "do" action is <input name="do" value="insertsettings" type="hidden">
With this new information we fix the uncached issue by adding$actiontemplates['insertsettings'] =& $actiontemplates['options'];below the $actiontemplates array.

The final code should look like this:$actiontemplates = array(
'options' => array(
'gtblog_options',
'gtblog_radio_option',
'newpost_errormessage',
)
$actiontemplates['insertsettings'] =& $actiontemplates['options'];

PennylessZ28
23 Jan 2006, 21:52
Good info, most people dont' follow these rules.

Princeton
23 Jan 2006, 22:30
gracias seņor...

feedback is always good and I do appreciate it

Snake
23 Jan 2006, 23:59
Thanks for the tutorial!

Marky
24 Jan 2006, 18:40
When creating a modification using a product, you can also use this method in a plugin.
Choose 'Cache_templates' and put the PHP code as:
$variablename[] = 'templatename';
For example, when creating a template called 'booyar' and you use $booyar to call the template, you can add a plugin with:
$booyar[] = 'booyar';

Hehe :)
Nice tutorial :)
Good job ^_^

CyberRanger
25 Jan 2006, 18:10
$actiontemplates are templates loaded when a specified action is called such as $do (e.g. ?do=edit)

I can't seem to get the right hang of this. If my script has different templates for different do actions (say ?do=edit, ?do=file, ?do=event, etc) how do I write the actiontemplates statement?

Princeton
25 Jan 2006, 18:16
$actiontemplates = array(
'edit' => array(
'template_name',
),
'file' => array(
'template_name',
),
'event' => array(
'template_name',
)
);

CyberRanger
25 Jan 2006, 18:19
$actiontemplates = array(
'edit' => array(
'template_name',
),
'file' => array(
'template_name',
),
'event' => array(
'template_name',
)
);

Wow ... thanks for the quick reply! So, is the ?do portion ignored or assumed? For example, would ?act=edit, ?act=file, ?act=event be coded exactly the same way?

Princeton
25 Jan 2006, 21:54
it should be ?do=edit, ?do=file, ?do=event

if you use anything else cache the template(s) using $globaltemplates

Chris-777
26 Jan 2006, 14:08
Question, since this issue just popped up on my forum with Calorie's photoplog thumb hack. (here (http://www.vbulletin.org/forum/showthread.php?t=103839&highlight=uncached)).

The only template call that I see in the .php file is this:


eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_photoplog_thumbs') . '";');


Would I just add $globaltemplates to the end of that, since it's not really an action template (it just displays thumbnails) or am I way off the mark?

Princeton
26 Jan 2006, 14:14
if it's a file add it to $globaltemplates array (found at the top of page):
$globaltemplates = array(
'other_templates_here',
'adv_portal_photoplog_thumbs',
);

Chris-777
26 Jan 2006, 14:18
Well, the template is called by the file, but I'm not sure exactly where to edit the $globaltemplates array itself.

Apologies for noobing up your thread, I've actually wondered how to do this for awhile now. ;)

Princeton
26 Jan 2006, 14:24
I don't really know anything about that hack but you can add the template to the cache_templates hook (plugin):
$globaltemplates[] = 'adv_portal_photoplog_thumbs';

I recommend adding some kind of condition to it.

Have you tried asking the author of the hack? It could just be something that he overlooked.

Chris-777
26 Jan 2006, 14:30
Yeah, I posted in his thread, I'm just trying to do it myself so that I know how in the future. I didn't even think of just adding it to the hook. Thanks man. :)

rom56
28 Jan 2006, 19:04
the geek articles use the template cache, i have make a mistake in my modification but i can edit it :s where can i delete this template cache ?

Princeton
28 Jan 2006, 19:09
I'm not sure what you mean. Have you asked the author of the hack?

netwind
29 Jan 2006, 14:44
princeton This article good for creating stand-alone pages, but I need wrote plugins, wich evalulate templates without modify standart forum code.
How ?

eval('$adv_code .= "' . fetch_template('adv_creativeSuite') . '";');

Princeton
29 Jan 2006, 20:52
netwind,
follow the instructions found on this page: http://www.vbulletin.org/forum/showthread.php?t=99132

puertoblack2003
11 Mar 2006, 16:08
ok trying to follow this rule but still confused which i'm learning to cache this template because i have this on my forum Uncached templates: Marquee_Text (1) ok where here do i add the fuction to elimated that uncache...

switch ($vbulletin->options['mtext_color'])
{
case 0: $renk = 'Dark Red'; break;
case 1: $renk = 'Red'; break;
case 2: $renk = 'Orange'; break;
case 3: $renk = 'Brown'; break;
case 4: $renk = 'Yellow'; break;
case 5: $renk = 'Green'; break;
case 6: $renk = 'Olive'; break;
case 7: $renk = 'Cyan'; break;
case 8: $renk = 'Blue'; break;
case 9: $renk = 'Dark Blue'; break;
case 10: $renk = 'Indigo'; break;
case 11: $renk = 'Violet'; break;
case 12: $renk = 'White'; break;
case 13: $renk = 'Black'; break;
}

switch ($vbulletin->options['mtext_direction'])
{
case 0: $yer = 'left'; break;
case 1: $yer = 'right'; break;
}

$Positionx1 = '$spacer_open';
eval('$marqtextx = "' . fetch_template('Marquee_Text') . '";');
$vbulletin->templatecache['header'] = str_replace($Positionx1,$Positionx1.'$marqtextx' , $vbulletin->templatecache['header']);

thank you

Princeton
11 Mar 2006, 18:00
add the template to the cache_templates hook (plugin):
$globaltemplates[] = 'Marquee_Text';

puertoblack2003
11 Mar 2006, 18:18
add the template to the cache_templates hook (plugin):
$globaltemplates[] = 'Marquee_Text';

thank you sir that worked ...

puertoblack2003
20 Mar 2006, 15:29
good day,

i using that method to cache template i implented a postbit marqee hack and for some reason that method won't work.

$globaltemplates[] = 'Postbit_Marquee';

this error is being shown in showthread any idea's to fix problem...

thanks in advance.

MRGTB
13 Sep 2006, 01:06
I just cant get my head around adding "cache" to a product, to cache the templates.

Will you do me a favour Princeton or anybody else who knows how to do this. Take a look at my product code and add the cache code for me. So I can just paste it back in my XML file.


<?xml version="1.0" encoding="ISO-8859-1"?>

<product productid="ed2k_links_hack" active="1">
<title>eD2K Links Hack</title>
<description>This will let you add eD2K Links to your forum board.</description>
<version>1.0.0</version>
<url>http://www.ed2k.biz</url>
<dependencies><dependency dependencytype="vbulletin" minversion="3.6.0" maxversion="" />
</dependencies>
<codes></codes>
<templates>
<template name="ed2k" templatetype="template" date="1133098897" username="eD2K" version="3.6.0"><![CDATA[<script type="text/javascript">
<!--
function checkAll(str,checked) {
var a = document.getElementsByName(str);
var n = a.length;
for (var i = 0; i < n; i++) {
a[i].checked = checked;
}
em_size(str);
}
function download(str, i, first) {
var a = document.getElementsByName(str);
var n = a.length;
for (var i = i; i < n; i++) {
if(a[i].checked) {
window.location=a[i].value;
if (first)
timeout = 6000;
else
timeout = 500;
i++;
window.setTimeout("download('"+str+"', "+i+", 0)", timeout);
break;
}
}
}
function copy(str) {
var a = document.getElementsByName(str);
var n = a.length;
var ed2kcopy = document.getElementById("ed2kcopy_"+str)
ed2kcopy.innerHTML = ""
for (var i = 0; i < n; i++) {
if(a[i].checked)
{
ed2kcopy.innerHTML += a[i].value;
ed2kcopy.innerHTML += "<br />";
}
}
var rng = document.body.createTextRange();
rng.moveToElementText(ed2kcopy)
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
function em_size(str) {
var a = document.getElementsByName(str);
var n = a.length;
try {
var input_checkall = document.getElementById("checkall_"+str);
var size = 0;
input_checkall.checked = true ;
for (var i=0; i < n; i++) {
if (a[i].checked) {
var piecesArray = a[i].value.split( "|" );
size += piecesArray[3]*1;
} else {
input_checkall.checked = false;
}
}
test = document.getElementById("size_"+str);
test.innerHTML = gen_size(size, 3, 2);
} catch (e) {
}
}
function gen_size(val, li, sepa ) {
sep = Math.pow(10, sepa);
li = Math.pow(10, li);
retval = val;
unit = 'Bytes';
if (val >= li*1000000000) {
val = Math.round( val / (1099511627776/sep) ) / sep;
unit = 'TB';
} else if (val >= li*1000000) {
val = Math.round( val / (1073741824/sep) ) / sep;
unit = 'GB';
} else if (val >= li*1000) {
val = Math.round( val / (1048576/sep) ) / sep;
unit = 'MB';
} else if (val >= li) {
val = Math.round( val / (1024/sep) ) / sep;
unit = 'KB';
}
return val + unit;
}
// -->
</script>
<br /><table class="tborder" cellpadding="5" cellspacing="1" border="0" width="100%" align="center">
<thead><tr><td class="tcat" colspan="2">eD2K Links</td></tr></thead><tbody>

$ed2kbit

<tr align="left" class="alt$key2"><td width="80%" align="left"><input type="checkbox" id="checkall_ed2k$rand" onclick="checkAll('ed2k$rand',this.checked)" checked="checked"/><label for="checkall_ed2k$rand">Select All</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Download Selected" onclick="download('ed2k$rand',0,1)" /> <input type="button" value="Copy Selected" onclick="copy('ed2k$rand')" /><div id="ed2kcopy_ed2k$rand" style="position:absolute;height:0px;width:0px;overflow:hidden;"></div></td><td align="center" id="size_ed2k$rand">Total Size: $total</td></tr></tbody></table>]]></template>
<template name="ed2kbit" templatetype="template" date="1133028805" username="eD2K" version="3.6.0"><![CDATA[<tr align="left" class="alt$key1"><td width="80%" align="left"><input type="checkbox" name="ed2k$rand" value="$ed2k" onclick="em_size('ed2k$rand');" checked="checked" /><a href="$ed2k">$ed2k_array[2]</a></td><td align="center">$totalper</td></tr>]]></template>
</templates>
<plugins>
<plugin active="1">
<title>eD2K Links Hack</title>
<hookname>bbcode_fetch_tags</hookname>
<phpcode><![CDATA[if ($vbulletin->options['allowedbbcodes'] & ALLOW_BBCODE_URL)
{
// [ED2K]
$tag_list['no_option']['ed2k'] = array(
'callback' => 'handle_ed2k_links',

);
}]]></phpcode>
</plugin>
</plugins>
<phrases>
</phrases>
<options>
</options>
</product>

pedroenf
23 Sep 2006, 14:40
Thanks for the explanation. It worked for me too.

RedTyger
16 Nov 2006, 15:55
If you have a lot of templates which may not necessarily be used, you can save resources that would be used to cache unneeded templates by instead caching them dynamically based on whether or not they are needed. This does assume a bit of knowledge, but it's commented for clarity.

Let's say you have 10 templates, all of which can be enabled or disabled individually via a yesno option in your plugin. I'll just display 3 for the example.


<plugin active="1" executionorder="5">
<title>Example</title>
<hookname>cache_templates</hookname>
<phpcode><![CDATA[
// Do nothing if the plugin is not globally enabled via your own plugin's yesno. You can leave this out entirely if you don't provide your own onoff switch
if ($vbulletin->options['myplugin_isenabled'])
{
// If option1 is enabled, add the template to a custom array, not the globaltemplates array. The array will be created by the adding if it doesn't already exist
if ($vbulletin->options['option1_isenabled'])
{
$mytemplates[] = 'mytemplate1';
}

// Do the same for every template that has a yesno on/off switch
if ($vbulletin->options['option2_isenabled'])
{
$mytemplates[] = 'mytemplate2';
}

// It doesn't have to be a switch, just something that needs to be true for your template to appear
if (in_array($what, $ever)
{
$mytemplates[] = 'mytemplate3';
}

// If there are more than 0 templates that are going to be used, merge everything that's been added to your custom array with the globaltemplates cache
if (count($mytemplates) > 0)
{
$globaltemplates = array_merge($globaltemplates, $mytemplates);
}
}
]]></phpcode>
</plugin>


The only problem with this is that if you have only one template needed, merging the arrays instead of just adding yours on (using $globaltemplates[] instead of $mytemplates) is wasteful of resources. However if you're using lots of templates this should not be an issue.

I would suggest using the microstats plugin to easily check everything is caching as it should.

Mudvayne
13 Jan 2007, 22:01
I need a little help.
if (THIS_SCRIPT == 'showthread' AND $vbulletin->options['quickreply'] > 0)
{
$globaltemplates = array_merge($globaltemplates, array(
'quickreply_addon',
));
}
This does catch template only in show thread page. But I need to cache the quickreply_addon template in new thread, new reply, pm reply/forward & infraction window too. What 'll be the code? Waiting for help.

Princeton
16 Jan 2007, 15:06
THIS_SCRIPT changes from file to file ...

at the top of any file you will find THIS_SCRIPT defined ... this is the value that needs to be used on the conditional

Mudvayne
16 Jan 2007, 15:17
I used another plugin to cache this template with this code.
$globaltemplates = array_merge($globaltemplates, array('quickreply_addon'));

thincom2000
19 Jan 2007, 20:34
When writing my plugin code, the following doesn't work in the cache_templates hook (queries still added for uncached templates):

$globaltemplates[] = 'my_extra_template';

But the following does:

$globaltemplates = array_merge( $globaltemplates, array( 'my_extra_template' ) );

OR

global $globaltemplates;

$globaltemplates[] = 'my_extra_template';

Also if I don't add the following to the parse_templates hook I get a spike in page generation time and server load:
eval('$my_extra_template = "' . fetch_template('my_extra_template') . '";');

Xplorer4x4
31 Jan 2007, 02:23
I need a little help.
if (THIS_SCRIPT == 'showthread' AND $vbulletin->options['quickreply'] > 0)
{
$globaltemplates = array_merge($globaltemplates, array(
'quickreply_addon',
));
}
This does catch template only in show thread page. But I need to cache the quickreply_addon template in new thread, new reply, pm reply/forward & infraction window too. What 'll be the code? Waiting for help.

This does not even cache the template on showthread for me. Can any one tell me why this is not caching on show thread? :( I been working for an hour atleast trying to cache this and it does not seem to work.

Muellmann
02 Jun 2007, 03:10
Very useful info. I followed the instructions to remove the uncached ones thx. :cool:

apn3a
12 Dec 2007, 05:29
i have a php script that i am having trouble with. it is not written for vbulletin, but i would like to include it in my vb's template. this script has about 3-4 php files. i create a php file from which i run my script (followed the instructions from How to create a vbulletin powered page). i only include once the index.php of my script, which runs other php files from inside of it.

my problem is the following: when i run the script, everything is fine, the template is there etc. when i enter the details the script asks for, and i hit submit, the new window that opens does not have the vb template. i've tried everything, no luck. the window that opens when i hit submit has no action (etc. xxx.php?=something.) its just xxx.php. the funny thing is that if i have an error in my form, the template stays as it is even though a new window opens. the problem only occurs when i hit submit. please help :(

noonespecial
22 Apr 2008, 22:04
My question is "Why"?

What does caching templates do? What benefit is there for the programmer and the end user?

Opserty
23 Apr 2008, 16:06
Caching templates means that all the templates are fetched with a single query at the beginning of the script, if you fetch un-cached templates during the script the script has to make database queries to fetch it. Thus using server resources that you don't need to.

Blackhat
04 Oct 2008, 11:49
add the template to the cache_templates hook (plugin):
$globaltemplates[] = 'Marquee_Text';

That works for me too. So I need to have to plugins for one template. One to call it and one for caching it, right ?

Call it

eval('$sidebar= "' . fetch_template('sidebar') . '";');


Cache it
$globaltemplates[] = 'sidebar';

and can I cache several templates in the same plugin ?

Thanks :)