mirror of
https://github.com/smarty-php/smarty.git
synced 2025-10-05 00:30:54 +02:00
131 lines
3.8 KiB
XML
131 lines
3.8 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>&$smarty</parameter></paramdef>
|
|
</funcprototype>
|
|
</funcsynopsis>
|
|
<para>
|
|
All <link linkend="language.syntax.attributes">attributes</link> 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, eg the
|
|
<link linkend="language.function.fetch"><varname>{fetch}</varname></link>
|
|
function. Alternatively, the function can simply perform some other
|
|
task without any output, eg the <link linkend="language.function.assign">
|
|
<varname>{assign}</varname></link> 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 eg
|
|
<literal>$smarty->foo()</literal>.
|
|
</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>
|
|
<para>
|
|
See also:
|
|
<link linkend="api.register.function"><varname>register_function()</varname></link>,
|
|
<link linkend="api.unregister.function"><varname>unregister_function()</varname></link>.
|
|
</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
|
|
-->
|