Files
smarty/docs/ru/programmers/plugins/plugins-block-functions.xml
2004-04-13 11:47:32 +00:00

95 lines
3.4 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="plugins.block.functions"><title>Block Functions</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>smarty_block_<replaceable>name</replaceable></function></funcdef>
<paramdef>array <parameter>$params</parameter></paramdef>
<paramdef>mixed <parameter>$content</parameter></paramdef>
<paramdef>object <parameter>&amp;$smarty</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Block functions are functions of the form: {func} .. {/func}. In other
words, they enclose a template block and operate on the contents of
this block. Block functions take precedence over custom functions of
the same name, that is, you cannot have both custom function {func} and
block function {func} .. {/func}.
</para>
<para>
Your function implementation is called twice by Smarty:
once for the opening tag, and once for the closing tag.
</para>
<para>
Only the opening tag of the block function may have attributes. All
attributes passed to template functions from the template are contained
in the <parameter>$params</parameter> as an associative array. You can
either access those values directly, e.g.
<varname>$params['start']</varname> or use
<varname>extract($params)</varname> to import them into the symbol
table. The opening tag attributes are also accessible to your function
when processing the closing tag.
</para>
<para>
The value of <parameter>$content</parameter> variable depends on
whether your function is called for the opening or closing tag. In case
of the opening tag, it will be <literal>null</literal>, and in case of
the closing tag it will be the contents of the template block.
Note that the template block will have already been processed by
Smarty, so all you will receive is the template output, not the
template source.
</para>
<para>
If you have nested block functions, it's possible to find out what the
parent block function is by accessing
<varname>$smarty->_tag_stack</varname> variable. Just do a var_dump()
on it and the structure should be apparent.
</para>
<para>
See also:
<link linkend="api.register.block">register_block()</link>,
<link linkend="api.unregister.block">unregister_block()</link>.
</para>
<example>
<title>block function</title>
<programlisting>
&lt;?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: block.translate.php
* Type: block
* Name: translate
* Purpose: translate a block of text
* -------------------------------------------------------------
*/
function smarty_block_translate($params, $content, &amp;$smarty)
{
if ($content) {
$lang = $params['lang'];
// do some intelligent translation thing here with $content
echo $translation;
}
}</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
-->