Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
registerPlugin()
2
 
3
dynamically register plugins
4
 
5
Description
6
===========
7
 
8
void
9
 
10
registerPlugin
11
 
12
string
13
 
14
type
15
 
16
string
17
 
18
name
19
 
20
mixed
21
 
22
callback
23
 
24
bool
25
 
26
cacheable
27
 
28
mixed
29
 
30
cache\_attrs
31
 
32
This method registers functions or methods defined in your script as
33
plugin. It uses the following parameters:
34
 
35
-   `cacheable` and `cache_attrs` can be omitted in most cases. See
36
    [controlling cacheability of plugins output](#caching.cacheable) on
37
    how to use them properly.
38
 
39
<!-- -->
40
 
41
 
42
    <?php
43
    $smarty->registerPlugin("function","date_now", "print_current_date");
44
 
45
    function print_current_date($params, $smarty)
46
    {
47
      if(empty($params["format"])) {
48
        $format = "%b %e, %Y";
49
      } else {
50
        $format = $params["format"];
51
      }
52
      return strftime($format,time());
53
    }
54
    ?>
55
 
56
 
57
 
58
And in the template
59
 
60
 
61
    {date_now}
62
 
63
    {* or to format differently *}
64
    {date_now format="%Y/%m/%d"}
65
 
66
 
67
    <?php
68
    // function declaration
69
    function do_translation ($params, $content, $smarty, &$repeat, $template)
70
    {
71
      if (isset($content)) {
72
        $lang = $params["lang"];
73
        // do some translation with $content
74
        return $translation;
75
      }
76
    }
77
 
78
    // register with smarty
79
    $smarty->registerPlugin("block","translate", "do_translation");
80
    ?>
81
 
82
 
83
 
84
Where the template is:
85
 
86
 
87
    {translate lang="br"}Hello, world!{/translate}
88
 
89
 
90
 
91
 
92
    <?php
93
 
94
    // let's map PHP's stripslashes function to a Smarty modifier.
95
    $smarty->registerPlugin("modifier","ss", "stripslashes");
96
 
97
    ?>
98
 
99
In the template, use `ss` to strip slashes.
100
 
101
 
102
    <?php
103
    {$var|ss}
104
    ?>
105
 
106
See also [`unregisterPlugin()`](#api.unregister.plugin), [plugin
107
functions](#plugins.functions), [plugin block
108
functions](#plugins.block.functions), [plugin compiler
109
functions](#plugins.compiler.functions), and the [creating plugin
110
modifiers](#plugins.modifiers) section.