Smarty Caching (again)
| From: | Rainer Collet | Date: | Thu, 29 Aug 2002 07:47:25 +0000 |
| Subject: | Smarty Caching (again) | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5866@lists.php.net to get a copy of this message | ||
hi all!
i already posted it some time ago and also in the wiki, but with no
response. perhaps because most of you were on vacation. so i'll try it
again.
smarty has a serious caching problem, when you use insert- or
template-functions that call $smarty->display or $smarty->fetch on their
own.
this is because of the $_cache_info variable in smarty. First, this variable
will be ok, but then the call to $smarty->display in a template-function
will reset $_cache_info['insert_tags'] so that the generated
cache/insert.tpl.php will get a wrong cache header viz without the
information in $_cache_info['insert_tags'].
so on the next execution of your script the insert_cache tag in the cached
file will not be evaluated, because of the missing
$_cache_info['insert_tags'] information in the cache header.
below i posted an example and a patch to fix it. i don't know if my patch
goes the right way, but if so, please take it in the next smarty release,
because i don't like to use a patched smarty version.
the second thing is, that i think smarty's caching mechanism is really weak
if you want to use it in a lage framework. if insert-functions are the only
way to have dynamic content in a cached template, you get problems with e.g.
simple if-cases that should not be cached. a much easier way would be to
have a block-function, say {nocache}...{/nocache}. i already tried to
implement it, but failed, because block-functions are cached, too. so i
think the only way would be to to extend smarty in a way.
i would do this job on my own, but i don't know where to start. the smarty
source code is poorly documented in this area. by the way, what do you think
of using phpdoc.org for documenting smarty source code?
perhaps someone could give me a hint how to implement the described
block-function. i saw it on the todo list, but what priority does it have?
do you already have ideas how to do the job?
thanks,
rainer
the following example shows how to crash smarty's caching mechanism:
insert.php
------------
require_once ( "Smarty.class.php" );
$smarty = new Smarty;
$smarty->caching = 1;
$smarty->cache_lifetime = 30;
$smarty->display ( "insert.tpl" );
------------
plugins/insert.test.php
------------
function smarty_insert_test($params, &$smarty)
{
extract ( $params );
return "TEXT:".$text;
}
------------
plugins/function.test.php
------------
function smarty_function_test($params, &$smarty)
{
//$cache_info = $smarty->_cache_info;
$smarty->display("test.tpl");
//$smarty->_cache_info = $cache_info;
}
------------
templates/insert.tpl
------------
{insert name="test" text="TEST"}<br>
{test}<br>
------------
templates/test.tpl
------------
TEST
------------
this is my patch which fixes the problem for me:
609a610,611
> if (isset($this->_cache_info)) $_cache_info = $this->_cache_info;
>
658a661
> if (isset($_cache_info)) $this->_cache_info = $_cache_info;
661a665
> if (isset($_cache_info)) $this->_cache_info = $_cache_info;
705a710,711
>
> if (isset($_cache_info)) $this->_cache_info = $_cache_info;