mirror of
https://github.com/smarty-php/smarty.git
synced 2025-10-31 20:31:41 +01:00
110 lines
3.5 KiB
XML
110 lines
3.5 KiB
XML
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
|||
|
|
<!-- $Revision$ -->
|
|||
|
|
<sect1 id="caching.cacheable">
|
|||
|
|
<title>Controlling Cacheability of Plugins' Output</title>
|
|||
|
|
<para>
|
|||
|
|
Desde Smarty-2.6.0 os caches de plugins pode ser declarados
|
|||
|
|
ao registr<74>-los. O terceiro par<61>metro para register_block,
|
|||
|
|
register_compiler_function e register_function <20> chamado
|
|||
|
|
<parameter>$cacheable</parameter> e o padr<64>o para true que <20> tamb<6D>m
|
|||
|
|
o comportamento de plugins na vers<72>o da Smarty antecessores <20> 2.6.0
|
|||
|
|
</para>
|
|||
|
|
|
|||
|
|
<para>
|
|||
|
|
Quando registrando um plugin com $cacheable=false o plugin <20> chamado todo o tempo na p<>gina que est<73> sendo mostrada, sempre se a p<>gina vier do cache. A fun<75><6E>o de plugin tem um comportamento levemente como uma fun<75><6E>o <link linkend="plugins.inserts">insert</link>.
|
|||
|
|
</para>
|
|||
|
|
|
|||
|
|
<para>
|
|||
|
|
Em contraste para <link linkend="language.function.insert">{insert}</link> o atributo para o plugin n<>o est<73> em cache por padr<64>o. Eles podem ser declarados para serem cacheados com o quarto par<61>metro <parameter>$cache_attrs</parameter>. <parameter>$cache_attrs</parameter> <20> um array de nomes de atributos que devem ser cacheados, ent<6E>o a fun<75><6E>o de plugin pega o valor como isso sendo o tempo que a p<>gina foi escrita para o cache todo o tempo isso <20> buscado do cache.
|
|||
|
|
</para>
|
|||
|
|
|
|||
|
|
<example>
|
|||
|
|
<title>Prevenindo uma sa<73>da de plugin de ser cacheada</title>
|
|||
|
|
<programlisting>
|
|||
|
|
index.php:
|
|||
|
|
|
|||
|
|
require('Smarty.class.php');
|
|||
|
|
$smarty = new Smarty;
|
|||
|
|
$smarty->caching = true;
|
|||
|
|
|
|||
|
|
function remaining_seconds($params, &$smarty) {
|
|||
|
|
$remain = $params['endtime'] - time();
|
|||
|
|
if ($remain >=0)
|
|||
|
|
return $remain . " second(s)";
|
|||
|
|
else
|
|||
|
|
return "done";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$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');
|
|||
|
|
|
|||
|
|
|
|||
|
|
index.tpl:
|
|||
|
|
|
|||
|
|
Tempo restante: {remain endtime=$obj->endtime}</programlisting>
|
|||
|
|
<para>
|
|||
|
|
O n<>mero de segundos at<61> que o endtime de $obj alcan<61>a altera<72><61>es em cada display de p<>gina, mesmo que a p<>gina esteja em cache. Desde o atributo endtime esteja em cache o objeto somente tem que ser puxado do banco de dados quando a p<>gina est<73> escrita para o cache mas n<>o em requisi<73><69>es subsequentes da p<>gina.
|
|||
|
|
</para>
|
|||
|
|
</example>
|
|||
|
|
|
|||
|
|
|
|||
|
|
<example>
|
|||
|
|
<title>Prevenindo uma passagem inteira do template para o cache</title>
|
|||
|
|
<programlisting>
|
|||
|
|
index.php:
|
|||
|
|
|
|||
|
|
require('Smarty.class.php');
|
|||
|
|
$smarty = new Smarty;
|
|||
|
|
$smarty->caching = true;
|
|||
|
|
|
|||
|
|
function smarty_block_dynamic($param, $content, &$smarty) {
|
|||
|
|
return $content;
|
|||
|
|
}
|
|||
|
|
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
|
|||
|
|
|
|||
|
|
$smarty->display('index.tpl');
|
|||
|
|
|
|||
|
|
|
|||
|
|
index.tpl:
|
|||
|
|
|
|||
|
|
Page created: {"0"|date_format:"%D %H:%M:%S"}
|
|||
|
|
|
|||
|
|
{dynamic}
|
|||
|
|
|
|||
|
|
Now is: {"0"|date_format:"%D %H:%M:%S"}
|
|||
|
|
|
|||
|
|
... do other stuff ...
|
|||
|
|
|
|||
|
|
{/dynamic}</programlisting>
|
|||
|
|
|
|||
|
|
</example>
|
|||
|
|
<para>
|
|||
|
|
Quando recarregado a p<>gina que voc<6F> ir<69> notar que ambas as datas diferem. Uma <20> "din<69>mica" e uma <20> "est<73>tica". Voc<6F> pode fazer qualquer coisa entre as tags {dynamic}...{/dynamic} e ter certeza que isso n<>o ir<69> ficar em cache como o restante da p<>gina.
|
|||
|
|
</para>
|
|||
|
|
</sect1>
|
|||
|
|
<!-- 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
|
|||
|
|
-->
|