Files
smarty/docs/en/programmers/plugins/plugins-functions.xml
2004-04-14 15:53:39 +00:00

127 lines
3.5 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="plugins.functions"><title>Template Functions</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>smarty_function_<replaceable>name</replaceable></function></funcdef>
<paramdef>array <parameter>$params</parameter></paramdef>
<paramdef>object <parameter>&amp;$smarty</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
All attributes passed to template functions from the template are
contained in the <parameter>$params</parameter> as an associative
array.
</para>
<para>
The output (return value) of the function will be substituted in place of the
function tag in the template (<function>fetch</function> function, for
example). Alternatively, the function can simply perform some other
task without any output (<function>assign</function> function).
</para>
<para>
If the function needs to assign some variables to the template or use
some other Smarty-provided functionality, it can use the supplied
<parameter>$smarty</parameter> object to do so.
</para>
<para>
See also:
<link linkend="api.register.function">register_function()</link>,
<link linkend="api.unregister.function">unregister_function()</link>.
</para>
<para>
<example>
<title>function plugin with output</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: function.eightball.php
* Type: function
* Name: eightball
* Purpose: outputs a random magic answer
* -------------------------------------------------------------
*/
function smarty_function_eightball($params, &$smarty)
{
$answers = array('Yes',
'No',
'No way',
'Outlook not so good',
'Ask again soon',
'Maybe in your reality');
$result = array_rand($answers);
return $answers[$result];
}
?>
]]>
</programlisting>
</example>
</para>
<para>
which can be used in the template as:
</para>
<programlisting>
Question: Will we ever have time travel?
Answer: {eightball}.
</programlisting>
<para>
<example>
<title>function plugin without output</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: function.assign.php
* Type: function
* Name: assign
* Purpose: assign a value to a template variable
* -------------------------------------------------------------
*/
function smarty_function_assign($params, &$smarty)
{
if (empty($params['var'])) {
$smarty->trigger_error("assign: missing 'var' parameter");
return;
}
if (!in_array('value', array_keys($params))) {
$smarty->trigger_error("assign: missing 'value' parameter");
return;
}
$smarty->assign($params['var'], $params['value']);
}
?>
]]>
</programlisting>
</example>
</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
-->