Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
Controlling Cacheability of Output {#caching.cacheable}
2
==================================
3
 
4
If caching is enabled normally the whole final output of the page gets
5
cached. However Smarty3 offers several options how to exclude sections
6
of your output from caching.
7
 
8
> **Note**
9
>
10
> Be sure any variables used within a non-cached section are also
11
> assigned from PHP when the page is loaded from the cache.
12
 
13
Cacheability of Template Section {#cacheability.sections}
14
--------------------------------
15
 
16
A larger section of your template can easily excluded from caching by
17
using the [`{nocache}`](#language.function.nocache) and
18
[`{/nocache}`](#language.function.nocache) tags.
19
 
20
 
21
 
22
    Today's date is
23
    {nocache}
24
    {$smarty.now|date_format}
25
    {/nocache}
26
 
27
 
28
 
29
The above code will output the current date on a cached page.
30
 
31
Cacheability of Tags {#cacheability.tags}
32
--------------------
33
 
34
Caching for an individual tag can be disabled by adding the \"nocache\"
35
option flag to the tag.
36
 
37
 
38
    Today's date is
39
    {$smarty.now|date_format nocache}
40
 
41
 
42
 
43
Cacheability of Variables {#cacheability.variables}
44
-------------------------
45
 
46
You can [`assign()`](#api.assign) variables as not cachable. Any tag
47
which uses such variable will be automatically executed in nocache mode.
48
 
49
> **Note**
50
>
51
> If a tag is executed in nocache mode you must make sure that all other
52
> variables used by that tag are also assigned from PHP when the page is
53
> loaded from the cache.
54
 
55
> **Note**
56
>
57
> The nocache status of an assigned variable will effect the compiled
58
> template code. If you change the status you must manually delete
59
> existing compiled and cached template files to force a recompile.
60
 
61
 
62
    // assign $foo as nocahe variable
63
    $smarty->assign('foo',time(),true);
64
 
65
 
66
    Dynamic time value is {$foo}
67
 
68
 
69
 
70
Cacheability of Plugins {#cacheability.plugins}
71
-----------------------
72
 
73
The cacheability of plugins can be declared when registering them. The
74
third parameter to [`registerPlugin()`](#api.register.plugin) is called
75
`$cacheable` and defaults to TRUE.
76
 
77
When registering a plugin with `$cacheable=false` the plugin is called
78
everytime the page is displayed, even if the page comes from the cache.
79
The plugin function behaves a little like an
80
[`{insert}`](#plugins.inserts) function.
81
 
82
> **Note**
83
>
84
> The `$cacheable` status will effect the compiled template code. If you
85
> change the status you must manually delete existing compiled and
86
> cached template files to force a recompile.
87
 
88
In contrast to [`{insert}`](#plugins.inserts) the attributes to the
89
plugins are not cached by default. They can be declared to be cached
90
with the fourth parameter `$cache_attrs`. `$cache_attrs` is an array of
91
attribute-names that should be cached, so the plugin-function get value
92
as it was the time the page was written to cache everytime it is fetched
93
from the cache.
94
 
95
 
96
    <?php
97
    $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
98
 
99
    function remaining_seconds($params, $smarty) {
100
        $remain = $params['endtime'] - time();
101
        if($remain >= 0){
102
            return $remain . ' second(s)';
103
        }else{
104
            return 'done';
105
        }
106
    }
107
 
108
    $smarty->registerPlugin('function','remaining', 'remaining_seconds', false, array('endtime'));
109
 
110
    if (!$smarty->isCached('index.tpl')) {
111
        // fetch $obj from db and assign...
112
        $smarty->assignByRef('obj', $obj);
113
    }
114
 
115
    $smarty->display('index.tpl');
116
    ?>
117
 
118
 
119
 
120
where `index.tpl` is:
121
 
122
 
123
    Time Remaining: {remaining endtime=$obj->endtime}
124
 
125
 
126
 
127
The number of seconds till the endtime of `$obj` is reached changes on
128
each display of the page, even if the page is cached. Since the endtime
129
attribute is cached the object only has to be pulled from the database
130
when page is written to the cache but not on subsequent requests of the
131
page.
132
 
133
 
134
    index.php:
135
 
136
    <?php
137
    $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
138
 
139
    function smarty_block_dynamic($param, $content, $smarty) {
140
        return $content;
141
    }
142
    $smarty->registerPlugin('block','dynamic', 'smarty_block_dynamic', false);
143
 
144
    $smarty->display('index.tpl');
145
    ?>
146
 
147
 
148
 
149
where `index.tpl` is:
150
 
151
 
152
    Page created: {'0'|date_format:'%D %H:%M:%S'}
153
 
154
    {dynamic}
155
 
156
    Now is: {'0'|date_format:'%D %H:%M:%S'}
157
 
158
    ... do other stuff ...
159
 
160
    {/dynamic}
161
 
162
 
163
 
164
When reloading the page you will notice that both dates differ. One is
165
"dynamic" one is "static". You can do everything between
166
`{dynamic}...{/dynamic}` and be sure it will not be cached like the rest
167
of the page.
168
 
169
> **Note**
170
>
171
> The above example shall just demonstrate how a dynamic block plugins
172
> works. See
173
> [`Cacheability of Template Section`](#cacheability.sections) on how to
174
> disable caching of a template section by the built-in
175
> [`{nocache}`](#language.function.nocache) and
176
> [`{/nocache}`](#language.function.nocache) tags.