vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   How-To Cache Templates (https://vborg.vbsupport.ru/showthread.php?t=104532)

Princeton 01-04-2006 10:00 PM

How-To Cache Templates
 
1 Attachment(s)
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']; 


PennylessZ28 01-23-2006 08:52 PM

Good info, most people dont' follow these rules.

Princeton 01-23-2006 09:30 PM

gracias se?or...

feedback is always good and I do appreciate it

Snake 01-23-2006 10:59 PM

Thanks for the tutorial!

Marky 01-24-2006 05:40 PM

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:
PHP Code:

$variablename[] = 'templatename'

For example, when creating a template called 'booyar' and you use $booyar to call the template, you can add a plugin with:
PHP Code:

$booyar[] = 'booyar'

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

CyberRanger 01-25-2006 05:10 PM

Quote:

Originally Posted by princeton
$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 01-25-2006 05:16 PM

HTML Code:

$actiontemplates = array(
    'edit' => array(
        'template_name',
    ),
    'file' => array(
        'template_name',
    ),
    'event' => array(
        'template_name',
    )
);


CyberRanger 01-25-2006 05:19 PM

Quote:

Originally Posted by princeton
HTML Code:

$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 01-25-2006 08:54 PM

it should be ?do=edit, ?do=file, ?do=event

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

Guest210212002 01-26-2006 01:08 PM

Question, since this issue just popped up on my forum with Calorie's photoplog thumb hack. (here).

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

Code:

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?


All times are GMT. The time now is 11:37 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02681 seconds
  • Memory Usage 1,755KB
  • 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
  • (1)bbcode_code_printable
  • (3)bbcode_html_printable
  • (6)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete