Updated resources docs.

This commit is contained in:
andrey
2002-02-19 22:11:50 +00:00
parent 0adc38dcff
commit c6b1c7f6bd

115
docs.sgml
View File

@@ -1022,7 +1022,7 @@ $smarty->unregister_modifier("strip_tags");
<funcprototype> <funcprototype>
<funcdef>void <function>register_resource</function></funcdef> <funcdef>void <function>register_resource</function></funcdef>
<paramdef>string <parameter>resource_name</parameter></paramdef> <paramdef>string <parameter>resource_name</parameter></paramdef>
<paramdef>string <parameter>function_name</parameter></paramdef> <paramdef>array <parameter>resource_funcs</parameter></paramdef>
</funcprototype> </funcprototype>
</funcsynopsis> </funcsynopsis>
<para> <para>
@@ -1036,7 +1036,10 @@ $smarty->unregister_modifier("strip_tags");
<title>register_resource</title> <title>register_resource</title>
<programlisting> <programlisting>
$smarty->register_resource("db","get_db_template"); $smarty->register_resource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
</programlisting> </programlisting>
</example> </example>
@@ -1502,53 +1505,107 @@ $smarty->display("file:F:/path/to/my/templates/menu.tpl");
</example> </example>
</sect2> </sect2>
<sect2> <sect2>
<title>Templates from a function call</title> <title>Templates from callbacks</title>
<para> <para>
You can get templates from a custom function call, such as You can retrieve templates using custom callbacks. Fetching
retrieving templates from a database. You do this by first templates from a database is one of the examples. You do this by
registering your resource handler function, then creating your creating the resource handler callbacks and then registering
function to get the template. Smarty expects your function to be of them with Smarty.
this form: funcname($tpl_name,&amp;$tpl_source, </para>
&amp;$tpl_timestamp[, boolean]) {} $tpl_name is passed into the
function, and your function should populate $tpl_source and <para>
$tpl_timestamp with the template source and the last modified time There are a total of 4 functions that need to be registered for
of the template respectively. The fourth parameter is a boolean each type of resource. Every function will receive the resource
value, used for determining if the source should be returned or name as the first parameter and the Smarty object as the last
not. Default this value to true in your function. If false gets parameter. The rest of parameters depend on the function.
passed, you only need to return the timestamp parameter. This was </para>
added to Smarty 1.4.6.
<para>
The first function is supposed to retrieve the resource. Its
second parameter is a variable passed by reference where the
source should be stored. The function is supposed to return
<literal>true</literal> if it was able to successfully retrieve
the source and <literal>false</literal> otherwise.
</para>
<para>
The second function is supposed to retrieve the last
modification time of the requested resource (as a UNIX
timestamp). The second parameter is a by reference variable
where the timestamp should be stored. The function is supposed
to return <literal>true</literal> if the timestamp could be
succesfully determined, and <literal>false</literal> otherwise.
</para>
<para>
The third function is supposed to return <literal>true</literal>
or <literal>false</literal>, depending on whether the requested
resource is secure or not. This function is not used for
non-template resources but should still be defined.
</para>
<para>
The fourth function is supposed to return
<literal>true</literal> or <literal>false</literal>, depending
on whether the requested resource is trusted or not. This
function is not used for template resources but should still be
defined.
</para> </para>
<example> <example>
<title>using templates from a function call</title> <title>Using template resource callbacks</title>
<programlisting> <programlisting>
// from PHP script // from PHP script
// put this function somewhere in your application // put these function somewhere in your application
function get_db_template ($tpl_name, &$tpl_source, &$tpl_timestamp, function db_get_template ($tpl_name, &amp;tpl_source, &amp;$smarty_obj)
$get_source=true, &amp;$smarty_obj) { {
// do database call here to fetch your template,
if($get_source) { // populating $tpl_source
// do database call here to fetch your template, populating
// $tpl_source and $tpl_timestamp.
$sql = new SQL; $sql = new SQL;
$sql->query("select tpl_source, tpl_timestamp from my_table $sql->query("select tpl_source
from my_table
where tpl_name='$tpl_name'"); where tpl_name='$tpl_name'");
extract($sql->record); if ($sql->num_rows) {
$tpl_source = $sql->record['tpl_source'];
return true;
} else { } else {
return false;
}
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
// do database call here to populate $tpl_timestamp. // do database call here to populate $tpl_timestamp.
$sql = new SQL; $sql = new SQL;
$sql->query("select tpl_timestamp from my_table $sql->query("select tpl_timestamp
from my_table
where tpl_name='$tpl_name'"); where tpl_name='$tpl_name'");
extract($sql->record); if ($sql->num_rows) {
$tpl_timestamp = $sql->record['tpl_timestamp'];
return true;
} else {
return false;
} }
}
function db_get_secure($tpl_name, &$smarty_obj)
{
// assume all templates are secure
return true; return true;
} }
function db_get_trusted($tpl_name, &$smarty_obj)
{
// not used
}
// register the resource name "db" // register the resource name "db"
$smarty->register_resource("db","get_db_template"); $smarty->register_resource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
// using resource from php script // using resource from php script
$smarty->display("db:index.tpl"); $smarty->display("db:index.tpl");