| 148 |
lars |
1 |
Block Functions {#plugins.block.functions}
|
|
|
2 |
===============
|
|
|
3 |
|
|
|
4 |
void
|
|
|
5 |
|
|
|
6 |
smarty\_block\_
|
|
|
7 |
|
|
|
8 |
name
|
|
|
9 |
|
|
|
10 |
array
|
|
|
11 |
|
|
|
12 |
\$params
|
|
|
13 |
|
|
|
14 |
mixed
|
|
|
15 |
|
|
|
16 |
\$content
|
|
|
17 |
|
|
|
18 |
object
|
|
|
19 |
|
|
|
20 |
\$template
|
|
|
21 |
|
|
|
22 |
boolean
|
|
|
23 |
|
|
|
24 |
&\$repeat
|
|
|
25 |
|
|
|
26 |
Block functions are functions of the form: `{func} .. {/func}`. In other
|
|
|
27 |
words, they enclose a template block and operate on the contents of this
|
|
|
28 |
block. Block functions take precedence over [custom
|
|
|
29 |
functions](#language.custom.functions) of the same name, that is, you
|
|
|
30 |
cannot have both custom function `{func}` and block function
|
|
|
31 |
`{func}..{/func}`.
|
|
|
32 |
|
|
|
33 |
- By default your function implementation is called twice by Smarty:
|
|
|
34 |
once for the opening tag, and once for the closing tag. (See
|
|
|
35 |
`$repeat` below on how to change this.)
|
|
|
36 |
|
|
|
37 |
- Starting with Smarty 3.1 the returned value of the opening tag call
|
|
|
38 |
is displayed as well.
|
|
|
39 |
|
|
|
40 |
- Only the opening tag of the block function may have
|
|
|
41 |
[attributes](#language.syntax.attributes). All attributes passed to
|
|
|
42 |
template functions from the template are contained in the `$params`
|
|
|
43 |
variable as an associative array. The opening tag attributes are
|
|
|
44 |
also accessible to your function when processing the closing tag.
|
|
|
45 |
|
|
|
46 |
- The value of the `$content` variable depends on whether your
|
|
|
47 |
function is called for the opening or closing tag. In case of the
|
|
|
48 |
opening tag, it will be NULL, and in case of the closing tag it will
|
|
|
49 |
be the contents of the template block. Note that the template block
|
|
|
50 |
will have already been processed by Smarty, so all you will receive
|
|
|
51 |
is the template output, not the template source.
|
|
|
52 |
|
|
|
53 |
- The parameter `$repeat` is passed by reference to the function
|
|
|
54 |
implementation and provides a possibility for it to control how many
|
|
|
55 |
times the block is displayed. By default `$repeat` is TRUE at the
|
|
|
56 |
first call of the block-function (the opening tag) and FALSE on all
|
|
|
57 |
subsequent calls to the block function (the block\'s closing tag).
|
|
|
58 |
Each time the function implementation returns with `$repeat` being
|
|
|
59 |
TRUE, the contents between `{func}...{/func}` are evaluated and the
|
|
|
60 |
function implementation is called again with the new block contents
|
|
|
61 |
in the parameter `$content`.
|
|
|
62 |
|
|
|
63 |
If you have nested block functions, it\'s possible to find out what the
|
|
|
64 |
parent block function is by accessing `$smarty->_tag_stack` variable.
|
| 991 |
lars |
65 |
Just do a [`var_dump()`](https://www.php.net/var_dump) on it and the
|
| 148 |
lars |
66 |
structure should be apparent.
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
<?php
|
|
|
70 |
/*
|
|
|
71 |
* Smarty plugin
|
|
|
72 |
* -------------------------------------------------------------
|
|
|
73 |
* File: block.translate.php
|
|
|
74 |
* Type: block
|
|
|
75 |
* Name: translate
|
|
|
76 |
* Purpose: translate a block of text
|
|
|
77 |
* -------------------------------------------------------------
|
|
|
78 |
*/
|
|
|
79 |
function smarty_block_translate($params, $content, Smarty_Internal_Template $template, &$repeat)
|
|
|
80 |
{
|
|
|
81 |
// only output on the closing tag
|
|
|
82 |
if(!$repeat){
|
|
|
83 |
if (isset($content)) {
|
|
|
84 |
$lang = $params['lang'];
|
|
|
85 |
// do some intelligent translation thing here with $content
|
|
|
86 |
return $translation;
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
?>
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
See also: [`registerPlugin()`](#api.register.plugin),
|
|
|
95 |
[`unregisterPlugin()`](#api.unregister.plugin).
|