Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles

Reply
 
Thread Tools
How-To Cache Templates
Princeton's Avatar
Princeton
Join Date: Nov 2001
Posts: 6,693

Joe Velez began developing for the web in 1998. He is an avid vBulletin user and volunteers his services as a vbulletin.org administrator. He currently spends his time maintaining and developing allnurses.com.

Vineland, NJ
Show Printable Version Email this Page Subscription
Princeton Princeton is offline 01-04-2006, 10:00 PM

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:
Quote:
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:
PHP Code:
$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:
PHP Code:
$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
HTML Code:
<input name="do" value="insertsettings" type="hidden">
With this new information we fix the uncached issue by adding
PHP Code:
$actiontemplates['insertsettings'] =& $actiontemplates['options']; 
below the $actiontemplates array.

The final code should look like this:
PHP Code:
$actiontemplates = array(
    
'options' => array(
        
'gtblog_options',
        
'gtblog_radio_option',
        
'newpost_errormessage',
    )
$actiontemplates['insertsettings'] =& $actiontemplates['options']; 
Attached Images
File Type: gif 1.gif (18.7 KB, 0 views)
File Type: gif 1a.gif (26.3 KB, 0 views)
Reply With Quote
  #22  
Old 03-11-2006, 05:18 PM
puertoblack2003's Avatar
puertoblack2003 puertoblack2003 is offline
 
Join Date: Aug 2005
Location: Philadelphia
Posts: 1,073
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Princeton
add the template to the cache_templates hook (plugin):
Code:
$globaltemplates[] = 'Marquee_Text';
thank you sir that worked ...
Reply With Quote
  #23  
Old 03-20-2006, 02:29 PM
puertoblack2003's Avatar
puertoblack2003 puertoblack2003 is offline
 
Join Date: Aug 2005
Location: Philadelphia
Posts: 1,073
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

good day,

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

Code:
$globaltemplates[] = 'Postbit_Marquee';
this error is being shown in showthread any idea's to fix problem...

thanks in advance.
Reply With Quote
  #24  
Old 09-13-2006, 12:06 AM
MRGTB MRGTB is offline
 
Join Date: Dec 2004
Posts: 548
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.

Code:
<?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>
Reply With Quote
  #25  
Old 09-23-2006, 01:40 PM
pedroenf pedroenf is offline
 
Join Date: Jun 2006
Location: Portugal
Posts: 308
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the explanation. It worked for me too.
Reply With Quote
  #26  
Old 11-16-2006, 02:55 PM
RedTyger's Avatar
RedTyger RedTyger is offline
 
Join Date: Nov 2006
Location: UK
Posts: 1,310
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.

PHP Code:
<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.
Reply With Quote
  #27  
Old 01-13-2007, 09:01 PM
Mudvayne's Avatar
Mudvayne Mudvayne is offline
 
Join Date: Dec 2005
Location: /dev/null/
Posts: 393
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I need a little help.
PHP Code:
                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.
Reply With Quote
  #28  
Old 01-16-2007, 02:06 PM
Princeton's Avatar
Princeton Princeton is offline
 
Join Date: Nov 2001
Location: Vineland, NJ
Posts: 6,693
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #29  
Old 01-16-2007, 02:17 PM
Mudvayne's Avatar
Mudvayne Mudvayne is offline
 
Join Date: Dec 2005
Location: /dev/null/
Posts: 393
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I used another plugin to cache this template with this code.
Code:
$globaltemplates = array_merge($globaltemplates, array('quickreply_addon'));
Reply With Quote
  #30  
Old 01-19-2007, 07:34 PM
thincom2000 thincom2000 is offline
 
Join Date: May 2006
Location: Bronx, NY
Posts: 1,205
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

PHP Code:
$globaltemplates[] = 'my_extra_template'
But the following does:

PHP Code:
$globaltemplates array_merge$globaltemplates, array( 'my_extra_template' ) ); 
OR

PHP Code:
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:
PHP Code:
eval('$my_extra_template = "' fetch_template('my_extra_template') . '";'); 
Reply With Quote
  #31  
Old 01-31-2007, 01:23 AM
Xplorer4x4 Xplorer4x4 is offline
 
Join Date: Apr 2005
Posts: 938
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Icy View Post
I need a little help.
PHP Code:
                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.
Reply With Quote
Reply

Thread Tools

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:54 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.04747 seconds
  • Memory Usage 2,367KB
  • Queries Executed 26 (?)
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
  • (4)bbcode_code
  • (1)bbcode_html
  • (11)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (3)pagenav_pagelink
  • (11)post_thanks_box
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (2)postbit_attachment
  • (11)postbit_onlinestatus
  • (11)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_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_attachment
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete