| 148 |
lars |
1 |
Template Functions {#plugins.functions}
|
|
|
2 |
==================
|
|
|
3 |
|
|
|
4 |
void
|
|
|
5 |
|
|
|
6 |
smarty\_function\_
|
|
|
7 |
|
|
|
8 |
name
|
|
|
9 |
|
|
|
10 |
array
|
|
|
11 |
|
|
|
12 |
\$params
|
|
|
13 |
|
|
|
14 |
object
|
|
|
15 |
|
|
|
16 |
\$template
|
|
|
17 |
|
|
|
18 |
All [attributes](#language.syntax.attributes) passed to template
|
|
|
19 |
functions from the template are contained in the `$params` as an
|
|
|
20 |
associative array.
|
|
|
21 |
|
|
|
22 |
The output (return value) of the function will be substituted in place
|
|
|
23 |
of the function tag in the template, eg the
|
|
|
24 |
[`{fetch}`](#language.function.fetch) function. Alternatively, the
|
|
|
25 |
function can simply perform some other task without any output, eg the
|
|
|
26 |
[`{assign}`](#language.function.assign) function.
|
|
|
27 |
|
|
|
28 |
If the function needs to assign some variables to the template or use
|
|
|
29 |
some other Smarty-provided functionality, it can use the supplied
|
|
|
30 |
`$template` object to do so eg `$template->foo()`.
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
<?php
|
|
|
34 |
/*
|
|
|
35 |
* Smarty plugin
|
|
|
36 |
* -------------------------------------------------------------
|
|
|
37 |
* File: function.eightball.php
|
|
|
38 |
* Type: function
|
|
|
39 |
* Name: eightball
|
|
|
40 |
* Purpose: outputs a random magic answer
|
|
|
41 |
* -------------------------------------------------------------
|
|
|
42 |
*/
|
|
|
43 |
function smarty_function_eightball($params, Smarty_Internal_Template $template)
|
|
|
44 |
{
|
|
|
45 |
$answers = array('Yes',
|
|
|
46 |
'No',
|
|
|
47 |
'No way',
|
|
|
48 |
'Outlook not so good',
|
|
|
49 |
'Ask again soon',
|
|
|
50 |
'Maybe in your reality');
|
|
|
51 |
|
|
|
52 |
$result = array_rand($answers);
|
|
|
53 |
return $answers[$result];
|
|
|
54 |
}
|
|
|
55 |
?>
|
|
|
56 |
|
|
|
57 |
which can be used in the template as:
|
|
|
58 |
|
|
|
59 |
Question: Will we ever have time travel?
|
|
|
60 |
Answer: {eightball}.
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
<?php
|
|
|
65 |
/*
|
|
|
66 |
* Smarty plugin
|
|
|
67 |
* -------------------------------------------------------------
|
|
|
68 |
* File: function.assign.php
|
|
|
69 |
* Type: function
|
|
|
70 |
* Name: assign
|
|
|
71 |
* Purpose: assign a value to a template variable
|
|
|
72 |
* -------------------------------------------------------------
|
|
|
73 |
*/
|
|
|
74 |
function smarty_function_assign($params, Smarty_Internal_Template $template)
|
|
|
75 |
{
|
|
|
76 |
if (empty($params['var'])) {
|
|
|
77 |
trigger_error("assign: missing 'var' parameter");
|
|
|
78 |
return;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
if (!in_array('value', array_keys($params))) {
|
|
|
82 |
trigger_error("assign: missing 'value' parameter");
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$template->assign($params['var'], $params['value']);
|
|
|
87 |
|
|
|
88 |
}
|
|
|
89 |
?>
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
See also: [`registerPlugin()`](#api.register.plugin),
|
|
|
94 |
[`unregisterPlugin()`](#api.unregister.plugin).
|