mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-01 12:51:38 +01:00
157 lines
5.6 KiB
XML
157 lines
5.6 KiB
XML
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 1.1 Maintainer: nobody Status: ready -->
|
|
<sect1 id="plugins.resources"><title>Recursos (Resources)</title>
|
|
<para>
|
|
Os plugins de Recursos são como uma forma genérica de fornecer códigos fontes de template
|
|
ou componentes de script PHP para a Smarty. Alguns exemplos de recursos:
|
|
banco de dados, LDAP, memória compartilhada, sockets, e assim por diante.
|
|
</para>
|
|
|
|
<para>
|
|
Há um total de 4 funções que precisam estar registradas
|
|
para cada tipo de recurso. Cada função receberá
|
|
o recurso requisitado como o primeiro parâmetro e o objeto da Smarty como o último parâmetro. O resto
|
|
dos parâmetros dependem da função.
|
|
</para>
|
|
|
|
<funcsynopsis>
|
|
<funcprototype>
|
|
<funcdef>bool <function>smarty_resource_<replaceable>name</replaceable>_source</function></funcdef>
|
|
<paramdef>string <parameter>$rsrc_name</parameter></paramdef>
|
|
<paramdef>string <parameter>&$source</parameter></paramdef>
|
|
<paramdef>object <parameter>&$smarty</parameter></paramdef>
|
|
</funcprototype>
|
|
<funcprototype>
|
|
<funcdef>bool <function>smarty_resource_<replaceable>name</replaceable>_timestamp</function></funcdef>
|
|
<paramdef>string <parameter>$rsrc_name</parameter></paramdef>
|
|
<paramdef>int <parameter>&$timestamp</parameter></paramdef>
|
|
<paramdef>object <parameter>&$smarty</parameter></paramdef>
|
|
</funcprototype>
|
|
<funcprototype>
|
|
<funcdef>bool <function>smarty_resource_<replaceable>name</replaceable>_secure</function></funcdef>
|
|
<paramdef>string <parameter>$rsrc_name</parameter></paramdef>
|
|
<paramdef>object <parameter>&$smarty</parameter></paramdef>
|
|
</funcprototype>
|
|
<funcprototype>
|
|
<funcdef>bool <function>smarty_resource_<replaceable>name</replaceable>_trusted</function></funcdef>
|
|
<paramdef>string <parameter>$rsrc_name</parameter></paramdef>
|
|
<paramdef>object <parameter>&$smarty</parameter></paramdef>
|
|
</funcprototype>
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
A primeira função deve devolver o recurso. Seu segundo parâmetro é uma variável passada por
|
|
referência onde o resultado seria armazenado.
|
|
A função deve retornar <literal>true</literal> se
|
|
ela está apta a devolver com sucesso o recurso e
|
|
caso contrário retorna <literal>false</literal>.
|
|
</para>
|
|
|
|
<para>
|
|
A segunda função deve devolver a última modificação do
|
|
recurso requisitado (como um timestamp Unix).
|
|
O segundo parâmetro é uma variável passada por referência onde o timestamp seria armazenado.
|
|
A função deve retornar <literal>true</literal>
|
|
se o timestamp poderia ser determinado com sucesso,
|
|
e caso contrário retornaria <literal>false</literal>.
|
|
</para>
|
|
|
|
<para>
|
|
A terceira função deve retornar <literal>true</literal> ou
|
|
<literal>false</literal>, dependendo do recurso requisitado
|
|
está seguro ou não. Esta função é usada
|
|
apenas para recursos de template mas ainda assim seria definida.
|
|
</para>
|
|
|
|
<para>
|
|
A quarta função deve retornar <literal>true</literal>
|
|
ou <literal>false</literal>, dependendo
|
|
do recurso requisitado ser confiável ou não.
|
|
Esta função é usada apenas para componentes de
|
|
script PHP requisitados pelas tags <command>include_php</command> ou
|
|
<command>insert</command> com o atributo <structfield>src</structfield>.
|
|
Entretanto, ela ainda assim mesmo seria definida para os recursos de template.
|
|
</para>
|
|
<para>
|
|
Veja também:
|
|
<link linkend="api.register.resource">register_resource()</link>,
|
|
<link linkend="api.unregister.resource">unregister_resource()</link>.
|
|
</para>
|
|
<example>
|
|
<title>Plugin resource (recurso)</title>
|
|
<programlisting>
|
|
<?php
|
|
/*
|
|
* Smarty plugin
|
|
* -------------------------------------------------------------
|
|
* File: resource.db.php
|
|
* Type: resource
|
|
* Name: db
|
|
* Purpose: Fetches templates from a database
|
|
* -------------------------------------------------------------
|
|
*/
|
|
function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty)
|
|
{
|
|
// do database call here to fetch your template,
|
|
// populating $tpl_source
|
|
$sql = new SQL;
|
|
$sql->query("select tpl_source
|
|
from my_table
|
|
where tpl_name='$tpl_name'");
|
|
if ($sql->num_rows) {
|
|
$tpl_source = $sql->record['tpl_source'];
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
|
|
{
|
|
// faz o banco de dados chamar aqui para preencher $tpl_timestamp.
|
|
$sql = new SQL;
|
|
$sql->query("select tpl_timestamp
|
|
from my_table
|
|
where tpl_name='$tpl_name'");
|
|
if ($sql->num_rows) {
|
|
$tpl_timestamp = $sql->record['tpl_timestamp'];
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function smarty_resource_db_secure($tpl_name, &$smarty)
|
|
{
|
|
// assume que todos os templates são seguros
|
|
return true;
|
|
}
|
|
|
|
function smarty_resource_db_trusted($tpl_name, &$smarty)
|
|
{
|
|
// não usado para templates
|
|
}
|
|
?></programlisting>
|
|
</example>
|
|
</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
|
|
--> |