| 148 |
lars |
1 |
Compiler Functions {#plugins.compiler.functions}
|
|
|
2 |
==================
|
|
|
3 |
|
|
|
4 |
Compiler functions are called only during compilation of the template.
|
|
|
5 |
They are useful for injecting PHP code or time-sensitive static content
|
|
|
6 |
into the template. If there is both a compiler function and a [custom
|
|
|
7 |
function](#language.custom.functions) registered under the same name,
|
|
|
8 |
the compiler function has precedence.
|
|
|
9 |
|
|
|
10 |
mixed
|
|
|
11 |
|
|
|
12 |
smarty\_compiler\_
|
|
|
13 |
|
|
|
14 |
name
|
|
|
15 |
|
|
|
16 |
array
|
|
|
17 |
|
|
|
18 |
\$params
|
|
|
19 |
|
|
|
20 |
object
|
|
|
21 |
|
|
|
22 |
\$smarty
|
|
|
23 |
|
|
|
24 |
The compiler function is passed two parameters: the params array which
|
|
|
25 |
contains precompiled strings for the attribute values and the Smarty
|
|
|
26 |
object. It\'s supposed to return the code to be injected into the
|
|
|
27 |
compiled template including the surrounding PHP tags.
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
<?php
|
|
|
31 |
/*
|
|
|
32 |
* Smarty plugin
|
|
|
33 |
* -------------------------------------------------------------
|
|
|
34 |
* File: compiler.tplheader.php
|
|
|
35 |
* Type: compiler
|
|
|
36 |
* Name: tplheader
|
|
|
37 |
* Purpose: Output header containing the source file name and
|
|
|
38 |
* the time it was compiled.
|
|
|
39 |
* -------------------------------------------------------------
|
|
|
40 |
*/
|
|
|
41 |
function smarty_compiler_tplheader($params, Smarty $smarty)
|
|
|
42 |
{
|
|
|
43 |
return "<?php\necho '" . $smarty->_current_file . " compiled at " . date('Y-m-d H:M'). "';\n?>";
|
|
|
44 |
}
|
|
|
45 |
?>
|
|
|
46 |
|
|
|
47 |
This function can be called from the template as:
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
{* this function gets executed at compile time only *}
|
|
|
51 |
{tplheader}
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
The resulting PHP code in the compiled template would be something like
|
|
|
56 |
this:
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
<?php
|
|
|
60 |
echo 'index.tpl compiled at 2002-02-20 20:02';
|
|
|
61 |
?>
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
See also [`registerPlugin()`](#api.register.plugin),
|
|
|
66 |
[`unregisterPlugin()`](#api.unregister.plugin).
|