Differences between smarty 2 and smarty 3)

Source: Internet
Author: User
Tags smarty template

Want to upgrade, but first understand 3 what is the difference between 2, find this article, the original address: http://minecz.blog.163.com/blog/static/12226148201032025849297/

However, when I downloaded smarty3, I found a complete official instruction document. The following is a simple translation. I recommend you read the official documentation.

The following are the differences between smarty2 and smarty3 compiled by colleagues.

The syntax structure of the smarty 3 API has been restructured to make it more consistent and modular. Although the syntax of smarty 2 is still supported for backward compatibility, a discarded notice will be thrown, although you can block this notice, it is strongly recommended that you use the 3 syntax when using smarty 3. On the one hand, the syntax of smarty 2 may be gradually canceled in later versions, on the other hand, the syntax of smarty2 is the encapsulation of the smarty3 API, so there will be performance loss.
? Differences between smarty3 and smarty
(1) Basically, the smarty3 method adopts the hump naming method, such as foobarbaz.
2) all smarty attributes have get and set methods, for example, $ smarty-> cache_dir = 'foo/'. Now you can assign $ smarty-> setcachedir ('foo /'), you can also get the attribute value through $ smarty-> getcachedir ().
3) smarty 3 abolished some methods such as "is *" because they are repeated with the current "get *" method.
4) smarty 3 can only run in PhP5, but does not support php4.
5) The {PHP} tag is disabled by default. use $ smarty-> allow_php_tag = true. Enable
6) The blank separator will not be parsed. For example, {Foo} will no longer be parsed as a smarty tag. You must use {Foo}

The following is an outline of the smarty3 API:
$ Smarty-> fetch ($ template, $ cache_id = NULL, $ compile_id = NULL, $ parent = NULL)
$ Smarty-> display ($ template, $ cache_id = NULL, $ compile_id = NULL, $ parent = NULL)
$ Smarty-> iscached ($ template, $ cache_id = NULL, $ compile_id = NULL)
$ Smarty-> createdata ($ parent = NULL)
$ Smarty-> createtemplate ($ template, $ cache_id = NULL, $ compile_id = NULL, $ parent = NULL)
$ Smarty-> enablesecurity ()
$ Smarty-> disablesecurity ()
$ Smarty-> settemplatedir ($ template_dir)
$ Smarty-> addtemplatedir ($ template_dir)
$ Smarty-> templateexists ($ resource_name)
$ Smarty-> loadplugin ($ plugin_name, $ check = true)
$ Smarty-> loadfilter ($ type, $ name)
$ Smarty-> setexceptionhandler ($ handler)
$ Smarty-> addpluginsdir ($ plugins_dir)
$ Smarty-> getglobal ($ varname = NULL)
$ Smarty-> getregisteredobject ($ name)
$ Smarty-> getdebugtemplate ()
$ Smarty-> setdebugtemplate ($ tpl_name)
$ Smarty-> assign ($ tpl_var, $ value = NULL, $ nocache = false, $ scope = smarty_local_scope)
$ Smarty-> assignglobal ($ varname, $ value = NULL, $ nocache = false)
$ Smarty-> assignbyref ($ tpl_var, & $ value, $ nocache = false, $ scope = smarty_local_scope)
$ Smarty-> append ($ tpl_var, $ value = NULL, $ merge = false, $ nocache = false, $ scope = smarty_local_scope)
$ Smarty-> appendbyref ($ tpl_var, & $ value, $ merge = false)
$ Smarty-> clearassign ($ tpl_var)
$ Smarty-> clearallassign ()

$ Smarty-> configload ($ config_file, $ sections = NULL)
$ Smarty-> getvariable ($ variable, $ _ PTR = NULL, $ search_parents = true, $ error_enable = true)
$ Smarty-> getconfigvariable ($ variable)
$ Smarty-> getstreamvariable ($ variable)
$ Smarty-> getconfigvars ($ varname = NULL)
$ Smarty-> clearconfig ($ varname = NULL)
$ Smarty-> gettemplatevars ($ varname = NULL, $ _ PTR = NULL, $ search_parents = true)

Some API calls will be moved to their own objects.
$ Smarty-> cache-> loadresource ($ type = NULL)
$ Smarty-> cache-> clearall ($ exp_time = NULL, $ type = NULL)
$ Smarty-> cache-> clear ($ template_name, $ cache_id = NULL, $ compile_id = NULL, $ exp_time = NULL, $ type = NULL)
$ Smarty-> Register-> block ($ block_tag, $ block_impl, $ cacheable = true, $ cache_attr = array ())
$ Smarty-> Register-> compilerfunction ($ compiler_tag, $ compiler_impl, $ cacheable = true)
$ Smarty-> Register-> templatefunction ($ function_tag, $ function_impl, $ cacheable = true, $ cache_attr = array ())
$ Smarty-> Register-> modifier ($ modifier_name, $ modifier_impl)
$ Smarty-> Register-> templateobject ($ object_name, $ object_impl, $ allowed = array (), $ smarty_args = true, $ block_methods = array ())
$ Smarty-> Register-> outputfilter ($ function_name)
$ Smarty-> Register-> postfilter ($ function_name)
$ Smarty-> Register-> prefilter ($ function_name)
$ Smarty-> Register-> Resource ($ resource_type, $ function_names)
$ Smarty-> Register-> variablefilter ($ function_name)
$ Smarty-> Register-> defaultpluginhandler ($ function_name)
$ Smarty-> Register-> defaulttemplatehandler ($ function_name)

$ smarty-> unregister-> block ($ block_tag)
$ smarty-> unregister-> compilerfunction ($ compiler_tag)
$ smarty-> unregister-> templatefunction ($ function_tag)
$ smarty-> unregister-> modifier ($ modifier)
$ smarty-> unregister-> templateobject ($ object_name)
$ smarty-> unregister-> outputfilter ($ function_name)
$ smarty-> unregister-> postfilter ($ function_name)
$ smarty-> unregister-> prefilter ($ function_name)
$ smarty-> unregister-> Resource ($ resource_type)
$ smarty-> unregister-> variablefilter ($ function_name)
$ smarty-> utility-> compilealltemplates ($ extention = '. TPL ', $ force_compile = false, $ time_limit = 0, $ max_errors = NULL)
$ smarty-> utility-> clearcompiledtemplate ($ resource_name = NULL, $ compile_id = NULL, $ exp_time = NULL)
$ smarty-> utility-> testinstall ()

get and set methods for some attributes
$ caching = $ smarty-> getcaching (); // get $ smarty-> caching
$ smarty-> setcaching (true); // set $ smarty-> caching
$ smarty-> setdeprecationnotices (false ); // set $ smarty-> deprecation_notices
$ smarty-> setcacheid ($ id ); // set $ smarty-> cache_id
$ debugging = $ smarty-> getdebugging (); // get $ smarty-> debugging

smarty3 has many new functions and features. These new features make it easier to use smarty.
1) smarty3 supports expressions in almost all places. If the security policy permits, expressions can even contain PHP functions and object methods and attributes, as shown in
? {$ X + $ y}
? {$ Foo = strlen ($ bar)}
? {Assign Var = Foo value = $ x + $ y}
? {$ Foo = myfunct ($ x + $ y) * 3)}
? {$ Foo [$ x + 3]}
2) the smarty tag can be used as the value of another tag, such as
? {$ Foo = {counter} + 3}
3) the smarty tag can be used in double quotation marks, for example,
? {$ Foo = "This Is Message {counter}"}
4) You can use arrays in the template as in PHP, assign values to a single element in the array or append an element to the array, for example,
? {Assign Var = Foo value = [1, 2, 3]}
? {Assign Var = Foo value = ['y' => 'yellow', 'B' => 'blue']}
? {Assign Var = Foo value = [1, [9, 8], 3]}
? {$ Foo ['bar'] = 1}
? {$ Foo ['bar'] ['blar '] = 1}
? Example: {$ Foo [] = 1}

5) the template variable name itself can be an expression
? $ Foo _ {$ bar}
? $ Foo _ {$ x + $ y}
? $ Foo _ {$ bar }_buh _ {$ Blar}
? {$ Foo _ {$ X }}< br> 6) implements the method chain of the object, as shown in
? {$ Object-> Method1 ($ X)-> method2 ($ Y)}
7) added the {for} tag, replacing {Section}
? {For $ x = 0, $ Y = count ($ Foo); $ x <$ Y; $ X ++}... {/for}
? {For $ x = $ start to $ end step $ step }... {/for}
8) inside the for loop, you can use a variable
? $ X @ iteration = number of iteration
? $ X @ Total = Total number of iterations
? $ X @ first = true on first iteration
? $ X @ last = true on last iteration
9) Use foreach as in PHP, for example,
{ foreach $ array as $ key =>$ Val}
{$ key }=>{$ Val }< BR >{/ foreach}
【 note: if the array subscript is used and enclosed in single quotes, conflicts with section usage are avoided.]
10) The while tag is added.
? {While $ Foo}... {/while}

{while $ x lt 10 }... {/while}
11) added the function tag
12) added the {nocache} Block Function and the nocache attribute to prevent the variable or function from being cached, you can also use the following method to avoid variable caching
? $ Smarty-> assign ('foo', $ something, true);
13) You can directly use a string to replace the smarty template, as shown in
? $ Smarty-> display ('string: This is my template, {$ Foo }! ');
? {Include file = "string: This is my template, {$ Foo }! "}
14) added the template inheritance function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.