View previous topic :: View next topic |
Author |
Message |
Diz-X Smarty Rookie
Joined: 20 Apr 2004 Posts: 7
|
Posted: Tue Apr 20, 2004 1:59 pm Post subject: REQ: Caching per included Template |
|
|
In my templates I use a lot {include file="template.tpl"} and I want some of those templates to be cached. I couldn't find a feature in the manual that does something like it. You can't define which templates to be cached and which not!
Would be nice if there was a function in php or in the template to cache the template untill expires.
Could this feature be added? Or are there any other ways to work arround this problem?
Thnx! |
|
Back to top |
|
electr0`n Guest
|
Posted: Fri Apr 23, 2004 5:24 pm Post subject: |
|
|
I think this feature would slow down the caching.
template:
Code: | {$header}
content
{$footer} |
[php:1:c2ae0d0748]<?php
$smarty = new Smarty();
// header
$smarty->caching = TRUE;
$smarty->cache_lifetime = -1;
$header = $smarty->fetch('header.tpl');
$smarty->assign('header', $header);
// footer
$smarty->caching = TRUE;
$smarty->cache_lifetime = 3600;
$footer = $smarty->fetch('footer.tpl');
$smarty->assign('footer', $footer);
$smarty->display('template.tpl');
?>[/php:1:c2ae0d0748]
With that solution, you can use different caching options. |
|
Back to top |
|
Diz-X Smarty Rookie
Joined: 20 Apr 2004 Posts: 7
|
Posted: Fri Apr 23, 2004 7:05 pm Post subject: |
|
|
It would be nice to have a feature like:
$smarty->cache("menu.tpl", 3600);
$smarty->cache("categories.tpl", 3600);
$smarty->display("index.tpl");
The 2 caching tpl's are included in the index.tpl. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Apr 23, 2004 8:15 pm Post subject: |
|
|
Diz-X, you can do something lke you suggest:
Assume caching is on and that ttl is set to 3600:
Code: | if (!$smarty->is_cached("menu.tpl") {
$smarty->fetch("menu.tpl"); // ensure that pages are cached
$smarty->fetch("categories.tpl"); // ensure that pages are pre-cached
}
$smarty->display("index.tpl"); |
Of course, since I've left the index.tpl at a ttl of 3600, the example is somewhat moot. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 30, 2004 8:02 am Post subject: |
|
|
@boots:
$smarty->assign('menu', $smarty->fetch('menu.tpl')); and {$menu}
would have a positive effect.
but if you simply have $smarty->fetch('menu.tpl') (to make the cached file be generated) and in the template do:
{include file="menu.tpl"} you are creating a cache image for menu.tpl unecessarily.
{include} is no alias for fetch().
{include} doesn't use the cached "menu.tpl" from above, but regenerates that part of index.tpl's output from scratch. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Apr 30, 2004 7:54 pm Post subject: |
|
|
messju wrote: | {include} is no alias for fetch() |
Really. I always assumed it did use cached results. Mea culpa, I guess -- but IMO, it SHOULD be an alias for fetch(). After all, when we write our own custom include plugins (as many have suggested on the forum) they often use fetch() internally... |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 30, 2004 8:03 pm Post subject: |
|
|
[quote="boots"] messju wrote: | After all, when we write our own custom include plugins (as many have suggested on the forum) they often use fetch() internally... |
Right, I also did that (both: suggested and wrote these plugins). Beware to not register any outputfilters if you do so, they'll run twice: one for the inner fetch() and one for the outer display. |
|
Back to top |
|
checat Smarty Rookie
Joined: 05 Feb 2004 Posts: 13 Location: Russia, Saint-Petersburg
|
Posted: Wed May 12, 2004 4:27 pm Post subject: |
|
|
In my projects on pages with different types of content which has different cache update time I use the following not-so-clean (possiblly, ugly!) plugins.
For parts of page updating rarely
{include_c file="some/template_name.html" cache_group="some|group" cache_lifetime=86400 other_template_var="text"}
For parts updating more often then the main page content
{insert name="include_c" file="other/template_name.html" cache_group="other|group" cache_lifetime=300 other_template_var="text"}
Plugins code
function.include_c.php:
[php:1:4ecaebc132]
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage custom_plugins
*/
/**
* Smarty {include_c} function plugin
*
* Type: function
* Name: {include_c}
* Purpose: make Smarty includes cachable
* @param array $params Format: array('file' => template resource name, 'cache_group'=> alternate cache group for included template, 'cache_lifetime'=> alternate cache lifetime, 'force_cache'=>smarty caching mode forced, 'assign' => variable name to assign output)
* @param object $smarty
* Note: if 'force_cache' is set, included template will be cached even if parent template is not cached
*/
function smarty_function_include_c($params, &$smarty)
{
if(empty($params['file']))
{
$smarty->trigger_error("include_c: missing 'file' parameter");
return;
}
else
{
$file=$params['file'];
unset($params['file']);
}
if(!empty($params['assign']))
{
$assign_name=$params['assign'];
unset($params['assign']);
}
$old_cache_lifetime=$smarty->cache_lifetime;
$old_caching=$smarty->caching;
if(isset($params['cache_lifetime']))
{
$smarty->cache_lifetime=base_view::randomize_lifetime($params['cache_lifetime']);
unset($params['cache_lifetime']);
}
if(isset($params['force_cache']))
{
$smarty->caching=$params['force_cache'];
unset($params['force_cache']);
}
if(isset($params['cache_group']))
{
$cache_id=$params['cache_group'];
unset($params['cache_group']);
}
else
{
$cache_id=NULL;
}
if(!empty($params))
{
// dirty: private var used
$_smarty_tpl_vars = $smarty->_tpl_vars;
$smarty->assign($params);
}
$result=$smarty->fetch($file,$cache_id);
$smarty->cache_lifetime=$old_cache_lifetime;
$smarty->caching=$old_caching;
if(!empty($params))
{
// dirty: private var used
$smarty->_tpl_vars = & $_smarty_tpl_vars;
}
if(isset($assign_name))
{
$smarty->assign($assign_name, $result);
return;
}
return $result;
}
?>
[/php:1:4ecaebc132]
insert.include_c.php:
[php:1:4ecaebc132]
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage custom_plugins
*/
/**
* cacheable template inserts
*
* Smarty plugin
* Type: insert
* Name: include_c
* Purpose: insert a template with small expire time
* in a page which has big expire time
*
*/
function smarty_insert_include_c($params, &$smarty)
{
require_once $smarty->_get_plugin_filepath('function','include_c');
return smarty_function_include_c($params,$smarty);
}
?>
[/php:1:4ecaebc132]
"No warranty for fitness for your purposes and blablabla" disclaimer assumed |
|
Back to top |
|
|