Files
smarty/docs/en/programmers/caching/caching-cacheable.xml

141 lines
4.0 KiB
XML
Raw Normal View History

2004-04-13 11:47:32 +00:00
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
2004-04-13 12:02:28 +00:00
<sect1 id="caching.cacheable">
<title>Controlling Cacheability of Plugins' Output</title>
2005-12-10 08:54:14 +00:00
<para>
Since Smarty-2.6.0 plugins the cacheability of plugins can be
2006-09-27 00:54:40 +00:00
declared when registering them. The third parameter to
<link linkend="api.register.block"><varname>register_block()</varname></link>,
<link linkend="api.register.compiler.function">
<varname>register_compiler_function()</varname></link> and <link
linkend="api.register.function"><varname>register_function()</varname></link>
is called <parameter>$cacheable</parameter> and defaults to &true; which
2005-12-10 08:54:14 +00:00
is also the behaviour of plugins in Smarty versions before 2.6.0
2004-04-13 12:14:17 +00:00
</para>
<para>
2006-09-27 00:54:40 +00:00
When registering a plugin with <literal>$cacheable=false</literal> the plugin
is called everytime the page is displayed, even if the page comes
2004-04-13 12:02:28 +00:00
from the cache. The plugin function behaves a little like an
2006-09-27 00:54:40 +00:00
<link linkend="plugins.inserts"><varname>{insert}</varname></link> function.
2004-04-13 12:14:17 +00:00
</para>
<para>
2006-09-27 00:54:40 +00:00
In contrast to <link linkend="plugins.inserts"><varname>{insert}</varname>
</link>
2004-04-13 12:02:28 +00:00
the attributes to the plugins are not cached by default. They can be
declared to be cached with the fourth parameter
2004-04-13 12:02:28 +00:00
<parameter>$cache_attrs</parameter>. <parameter>$cache_attrs</parameter>
is an array of attribute-names that should be cached, so the
plugin-function get value as it was the time the page was written
2004-04-13 12:02:28 +00:00
to cache everytime it is fetched from the cache.
2004-04-13 12:14:17 +00:00
</para>
2004-04-13 12:02:28 +00:00
2004-04-13 12:14:17 +00:00
<example>
2004-04-13 12:02:28 +00:00
<title>Preventing a plugin's output from being cached</title>
<programlisting role="php">
2004-04-13 11:47:32 +00:00
<![CDATA[
<?php
$smarty->caching = true;
function remaining_seconds($params, &$smarty) {
$remain = $params['endtime'] - time();
2006-02-17 22:19:14 +00:00
if($remain >= 0){
return $remain . ' second(s)';
}else{
return 'done';
}
2004-04-13 11:47:32 +00:00
}
$smarty->register_function('remaining', 'remaining_seconds', false, array('endtime'));
if (!$smarty->is_cached('index.tpl')) {
// fetch $obj from db and assign...
$smarty->assign_by_ref('obj', $obj);
}
$smarty->display('index.tpl');
?>
2004-07-16 12:24:51 +00:00
]]>
2004-05-29 21:22:39 +00:00
</programlisting>
<para>
2006-09-27 00:54:40 +00:00
where <filename>index.tpl</filename> is:
2004-05-29 21:22:39 +00:00
</para>
<programlisting>
<![CDATA[
2005-01-05 14:37:31 +00:00
Time Remaining: {remaining endtime=$obj->endtime}
2004-04-13 11:47:32 +00:00
]]>
2004-04-13 12:02:28 +00:00
</programlisting>
2004-04-13 12:14:17 +00:00
<para>
2006-09-27 00:54:40 +00:00
The number of seconds till the endtime of <literal>$obj</literal> is reached
changes on each display of the page, even if the page is cached. Since the
2004-04-13 12:02:28 +00:00
endtime attribute is cached the object only has to be pulled from the
database when page is written to the cache but not on subsequent requests
of the page.
2004-04-13 12:14:17 +00:00
</para>
2004-04-13 12:02:28 +00:00
</example>
2004-04-13 12:14:17 +00:00
<example>
2004-04-13 12:02:28 +00:00
<title>Preventing a whole passage of a template from being cached</title>
<programlisting role="php">
2004-04-13 11:47:32 +00:00
<![CDATA[
index.php:
<?php
2006-09-27 00:54:40 +00:00
$smarty->caching = 1;
2004-04-13 11:47:32 +00:00
function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
$smarty->display('index.tpl');
?>
2004-05-29 21:22:39 +00:00
]]>
</programlisting>
<para>
2006-09-27 00:54:40 +00:00
where <filename>index.tpl</filename> is:
2004-05-29 21:22:39 +00:00
</para>
<programlisting>
<![CDATA[
2006-09-27 00:54:40 +00:00
Page created: {'0'|date_format:'%D %H:%M:%S'}
2004-04-13 11:47:32 +00:00
{dynamic}
2006-09-27 00:54:40 +00:00
Now is: {'0'|date_format:'%D %H:%M:%S'}
2004-04-13 11:47:32 +00:00
... do other stuff ...
{/dynamic}
]]>
2004-04-13 12:02:28 +00:00
</programlisting>
2004-04-13 12:14:17 +00:00
</example>
2004-04-13 12:14:17 +00:00
<para>
2004-04-13 12:02:28 +00:00
When reloading the page you will notice that both dates differ. One
2006-09-27 00:54:40 +00:00
is <quote>dynamic</quote> one is <quote>static</quote>. You can do everything
between <literal>{dynamic}...{/dynamic}</literal> and be sure it will not
be cached like the rest of the page.
2004-04-13 12:14:17 +00:00
</para>
2004-04-13 12:02:28 +00:00
</sect1>
2004-04-13 11:47:32 +00:00
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
2004-04-13 12:02:28 +00:00
-->