source: trunk/include/smarty/SMARTY_3.1_NOTES.txt @ 27403

Last change on this file since 27403 was 23384, checked in by rvelices, 11 years ago

smarty 3 - first pass for tests

File size: 11.3 KB
Line 
1Smarty 3.1 Notes
2================
3
4Smarty 3.1 is a departure from 2.0 compatibility. Most notably, all
5backward compatibility has been moved to a separate class file named
6SmartyBC.class.php. If you require compatibility with 2.0, you will
7need to use this class.
8
9Some differences from 3.0 are also present. 3.1 begins the journey of
10requiring setters/getters for property access. So far this is only
11implemented on the five directory properties: template_dir,
12plugins_dir, configs_dir, compile_dir and cache_dir. These properties
13are now protected, it is required to use the setters/getters instead.
14That said, direct property access will still work, however slightly
15slower since they will now fall through __set() and __get() and in
16turn passed through the setter/getter methods. 3.2 will exhibit a full
17list of setter/getter methods for all (currently) public properties,
18so code-completion in your IDE will work as expected.
19
20There is absolutely no PHP allowed in templates any more. All
21deprecated features of Smarty 2.0 are gone. Again, use the SmartyBC
22class if you need any backward compatibility.
23
24Internal Changes
25
26  Full UTF-8 Compatibility
27
28The plugins shipped with Smarty 3.1 have been rewritten to fully
29support UTF-8 strings if Multibyte String is available. Without
30MBString UTF-8 cannot be handled properly. For those rare cases where
31templates themselves have to juggle encodings, the new modifiers
32to_charset and from_charset may come in handy.
33
34  Plugin API and Performance
35
36All Plugins (modifiers, functions, blocks, resources,
37default_template_handlers, etc) are now receiving the
38Smarty_Internal_Template instance, where they were supplied with the
39Smarty instance in Smarty 3.0. *. As The Smarty_Internal_Template
40mimics the behavior of Smarty, this API simplification should not
41require any changes to custom plugins.
42
43The plugins shipped with Smarty 3.1 have been rewritten for better
44performance. Most notably {html_select_date} and {html_select_time}
45have been improved vastly. Performance aside, plugins have also been
46reviewed and generalized in their API. {html_select_date} and
47{html_select_time} now share almost all available options.
48
49The escape modifier now knows the $double_encode option, which will
50prevent entities from being encoded again.
51
52The capitalize modifier now know the $lc_rest option, which makes sure
53all letters following a captial letter are lower-cased.
54
55The count_sentences modifier now accepts (.?!) as
56legitimate endings of a sentence - previously only (.) was
57accepted
58
59The new unescape modifier is there to reverse the effects of the
60escape modifier. This applies to the escape formats html, htmlall and
61entity.
62
63  default_template_handler_func
64
65The invocation of $smarty->$default_template_handler_func had to be
66altered. Instead of a Smarty_Internal_Template, the fifth argument is
67now provided with the Smarty instance. New footprint:
68
69
70/**
71 * Default Template Handler
72 *
73 * called when Smarty's file: resource is unable to load a requested file
74 *
75 * @param string   $type     resource type (e.g. "file", "string", "eval", "resource")
76 * @param string   $name     resource name (e.g. "foo/bar.tpl")
77 * @param string  &$content  template's content
78 * @param integer &$modified template's modification time
79 * @param Smarty   $smarty   Smarty instance
80 * @return string|boolean   path to file or boolean true if $content and $modified
81 *                          have been filled, boolean false if no default template
82 *                          could be loaded
83 */
84function default_template_handler_func($type, $name, &$content, &$modified, Smarty $smarty) {
85    if (false) {
86        // return corrected filepath
87        return "/tmp/some/foobar.tpl";
88    } elseif (false) {
89        // return a template directly
90        $content = "the template source";
91        $modified = time();
92        return true;
93    } else {
94        // tell smarty that we failed
95        return false;
96    }
97}
98
99  Stuff done to the compiler
100
101Many performance improvements have happened internally. One notable
102improvement is that all compiled templates are now handled as PHP
103functions. This speeds up repeated templates tremendously, as each one
104calls an (in-memory) PHP function instead of performing another file
105include/scan.
106
107New Features
108
109  Template syntax
110
111 {block}..{/block}
112
113The {block} tag has a new hide option flag. It does suppress the block
114content if no corresponding child block exists.
115EXAMPLE:
116parent.tpl
117{block name=body hide} child content "{$smarty.block.child}" was
118inserted {block}
119In the above example the whole block will be suppressed if no child
120block "body" is existing.
121
122 {setfilter}..{/setfilter}
123
124The new {setfilter} block tag allows the definition of filters which
125run on variable output.
126SYNTAX:
127{setfilter filter1|filter2|filter3....}
128Smarty3 will lookup up matching filters in the following search order:
1291. varibale filter plugin in plugins_dir.
1302. a valid modifier. A modifier specification will also accept
131additional parameter like filter2:'foo'
1323. a PHP function
133{/setfilter} will turn previous filter setting off again.
134{setfilter} tags can be nested.
135EXAMPLE:
136{setfilter filter1}
137  {$foo}
138  {setfilter filter2}
139    {$bar}
140  {/setfilter}
141  {$buh}
142{/setfilter}
143{$blar}
144In the above example filter1 will run on the output of $foo, filter2
145on $bar, filter1 again on $buh and no filter on $blar.
146NOTES:
147- {$foo nofilter} will suppress the filters
148- These filters will run in addition to filters defined by
149registerFilter('variable',...), autoLoadFilter('variable',...) and
150defined default modifier.
151- {setfilter} will effect only the current template, not included
152subtemplates.
153
154  Resource API
155
156Smarty 3.1 features a new approach to resource management. The
157Smarty_Resource API allows simple, yet powerful integration of custom
158resources for templates and configuration files. It offers simple
159functions for loading data from a custom resource (e.g. database) as
160well as define new template types adhering to the special
161non-compiling (e,g, plain php) and non-compile-caching (e.g. eval:
162resource type) resources.
163
164See demo/plugins/resource.mysql.php for an example custom database
165resource.
166
167Note that old-fashioned registration of callbacks for resource
168management has been deprecated but is still possible with SmartyBC.
169
170  CacheResource API
171
172In line with the Resource API, the CacheResource API offers a more
173comfortable handling of output-cache data. With the
174Smarty_CacheResource_Custom accessing databases is made simple. With
175the introduction of Smarty_CacheResource_KeyValueStore the
176implementation of resources like memcache or APC became a no-brainer;
177simple hash-based storage systems are now supporting hierarchical
178output-caches.
179
180See demo/plugins/cacheresource.mysql.php for an example custom
181database CacheResource.
182See demo/plugins/cacheresource.memcache.php for an example custom
183memcache CacheResource using the KeyValueStore helper.
184
185Note that old-fashioned registration of $cache_handler is not possible
186anymore. As the functionality had not been ported to Smarty 3.0.x
187properly, it has been dropped from 3.1 completely.
188
189Locking facilities have been implemented to avoid concurrent cache
190generation. Enable cache locking by setting
191$smarty->cache_locking = true;
192
193  Relative Paths in Templates (File-Resource)
194
195As of Smarty 3.1 {include file="../foo.tpl"} and {include
196file="./foo.tpl"} will resolve relative to the template they're in.
197Relative paths are available with {include file="..."} and
198{extends file="..."}. As $smarty->fetch('../foo.tpl') and
199$smarty->fetch('./foo.tpl') cannot be relative to a template, an
200exception is thrown.
201
202  Adressing a specific $template_dir
203
204Smarty 3.1 introduces the $template_dir index notation.
205$smarty->fetch('[foo]bar.tpl') and {include file="[foo]bar.tpl"}
206require the template bar.tpl to be loaded from $template_dir['foo'];
207Smarty::setTemplateDir() and Smarty::addTemplateDir() offer ways to
208define indexes along with the actual directories.
209
210  Mixing Resources in extends-Resource
211
212Taking the php extends: template resource one step further, it is now
213possible to mix resources within an extends: call like
214$smarty->fetch("extends:file:foo.tpl|db:bar.tpl");
215
216To make eval: and string: resources available to the inheritance
217chain, eval:base64:TPL_STRING and eval:urlencode:TPL_STRING have been
218introduced. Supplying the base64 or urlencode flags will trigger
219decoding the TPL_STRING in with either base64_decode() or urldecode().
220
221  extends-Resource in template inheritance
222
223Template based inheritance may now inherit from php's extends:
224resource like {extends file="extends:foo.tpl|db:bar.tpl"}.
225
226  New Smarty property escape_html
227
228$smarty->escape_html = true will autoescape all template variable
229output by calling htmlspecialchars({$output}, ENT_QUOTES,
230SMARTY_RESOURCE_CHAR_SET).
231NOTE:
232This is a compile time option. If you change the setting you must make
233sure that the templates get recompiled.
234
235  New option at Smarty property compile_check
236
237The automatic recompilation of modified templates can now be
238controlled by the following settings:
239$smarty->compile_check = COMPILECHECK_OFF (false) - template files
240will not be checked
241$smarty->compile_check = COMPILECHECK_ON (true) - template files will
242always be checked
243$smarty->compile_check = COMPILECHECK_CACHEMISS - template files will
244be checked if caching is enabled and there is no existing cache file
245or it has expired
246
247  Automatic recompilation on Smarty version change
248
249Templates will now be automatically recompiled on Smarty version
250changes to avoide incompatibillities in the compiled code. Compiled
251template checked against the current setting of the SMARTY_VERSION
252constant.
253
254  default_config_handler_func()
255
256Analogous to the default_template_handler_func()
257default_config_handler_func() has been introduced.
258
259  default_plugin_handler_func()
260
261An optional default_plugin_handler_func() can be defined which gets called
262by the compiler on tags which can't be resolved internally or by plugins.
263The default_plugin_handler() can map tags to plugins on the fly.
264
265New getters/setters
266
267The following setters/getters will be part of the official
268documentation, and will be strongly recommended. Direct property
269access will still work for the foreseeable future... it will be
270transparently routed through the setters/getters, and consequently a
271bit slower.
272
273array|string getTemplateDir( [string $index] )
274replaces $smarty->template_dir; and $smarty->template_dir[$index];
275Smarty setTemplateDir( array|string $path )
276replaces $smarty->template_dir = "foo"; and $smarty->template_dir =
277array("foo", "bar");
278Smarty addTemplateDir( array|string $path, [string $index])
279replaces $smarty->template_dir[] = "bar"; and
280$smarty->template_dir[$index] = "bar";
281
282array|string getConfigDir( [string $index] )
283replaces $smarty->config_dir; and $smarty->config_dir[$index];
284Smarty setConfigDir( array|string $path )
285replaces $smarty->config_dir = "foo"; and $smarty->config_dir =
286array("foo", "bar");
287Smarty addConfigDir( array|string $path, [string $index])
288replaces $smarty->config_dir[] = "bar"; and
289$smarty->config_dir[$index] = "bar";
290
291array getPluginsDir()
292replaces $smarty->plugins_dir;
293Smarty setPluginsDir( array|string $path )
294replaces $smarty->plugins_dir = "foo";
295Smarty addPluginsDir( array|string $path )
296replaces $smarty->plugins_dir[] = "bar";
297
298string getCompileDir()
299replaces $smarty->compile_dir;
300Smarty setCompileDir( string $path )
301replaces $smarty->compile_dir = "foo";
302
303string getCacheDir()
304replaces $smarty->cache_dir;
305Smarty setCacheDir( string $path )
306replaces $smarty->cache_dir;
Note: See TracBrowser for help on using the repository browser.