PHP Learning smarty----call variable to go

Source: Internet
Author: User

PHP Learning smarty----Calling variables

Knowledge collection and management of front-end engineers PHP Learning smarty------SMARTY3

2010-06-29 16:47:05| Category: Php+ajax | report | font size Subscription

I. Overview

Smarty3 currently the latest version is RC1, not yet stable.

Smarty3 basically keeps forward compatible, but there are several changes:

1. Smarty3 need php5, no longer compatible with PHP4;

2. Smarty3 the {PHP} tag is disabled by default, if you want to enable the setting: $smarty->allow_php_tag = true;

3. Now, there can be no whitespace in both ends of the delimiter, such as: {foreach} does not match the foreach tag, but instead directly outputs "{foreach}", so {literal} This tag is not required;

4. Smarty3 API has a lot of changes, although the Smarty2 API is still available, but it has not been recommended. two. New features of the Smarty3 template engine

1. Expressions can be used anywhere, and functions in PHP can be used directly in an expression. In this way, the previous math function is largely unused. Such as:

{$x + $y}

Would output the sum of x and Y

{$foo = strlen ($bar)}

function in Assignment

{Assign Var=foo value= $x + $y}

In attributes

{$foo = Myfunct (($x + $y)}

as function parameter

{$foo [$x +3]}

As array index

2. Another label can be used in a label. For example: {$foo = ${counter} + 3}

3. Labels can also be used in double quotes. For example: {$foo = "This is message {counter}"}

4. Define the data directly in the template. Such as:

{Assign Var=foo value=[1,2,3]}

{Assign Var=foo value=[' y ' = ' yellow ', ' b ' = ' Blue ']}

{Assign Var=foo value=[1,[9,8],3]} {* Array can be multilayer *}

5. Assign variable is easier, do not need to use assign tag, for example: {$foo = $bar + 2}

6. The following notation is also supported, and is automatically converted to an array of requirements before assignment:

{$foo [' bar '] = 1}

{$foo [' bar '] [' blar '] = 2}

{$foo = 1}

{$foo [] = 1}

7. The following two formulations are equivalent:

{$foo. A.B.C}

$foo [' A '] [' B '] [' C ']

{$foo. A. $b. C}

$foo [' A '] [$b] [' C ']

{$foo. A. {$b +4}.C}

$foo [' A '] [$b +4][' C ']

{$foo. A. {$b. C}}

$foo [' A '] [$b [' C ']]

8. Variable names can contain variables or expressions, which is powerful. Such as:

$foo

Normal variable

$foo _{$bar}

Variable name containing other variable

$foo _{$x + $y}

Variable name containing expressions

$foo _{$bar}_buh_{$blar}

Variable name with multiple segments

{$foo _{$x}}

Would output the variable $foo _1 if $x has a value of 1.


9. Support for chained calls to objects, such as: {$object->method1 ($x)->method2 ($y)}

10. Added for this tag to replace section (but section is still supported), such as:

{for $x =0, $y =count ($foo), $x < $y; $x + +} .... {/for}

In particular, the for also supports the following usage:

{for $x = $start to $end step $step} ...   {/for} {* Note: It looks like the official version only supports step*}

{for $x = $start to $end} ... {/for}

Also, in the loop, the following variables are available:

[Email protected]

Number of iteration

[Email protected]

Total number of iterations

[Email protected]

True on first iteration

[Email protected]

True on last iteration


One. Foreach is also improved (the previous foreach notation continues to be supported), such as: {foreach $myarray as $var} ... {/foreach}

In the Foreach loop, the following variables are available:

[Email protected]

foreach $var array key

[Email protected]

foreach Current iteration count (...)

[Email protected]

foreach Current index count (0,1,2 ...)

[Email protected]

foreach $var Array Total

[Email protected]

True on first iteration

[Email protected]

True on last iteration

12. Added a while tag, such as:

{While $foo} ... {/while}

{while $x lt 10} ... {/while}

13. Support for direct use of functions in PHP (so {php}{/php} is not so important), such as the template can support the write:

{Time ()}

---

{function Name=menu level=0}

<ulclass= "level{$level}" >

{foreach $data as $entry}

{if Is_array ($entry)}

<li>{[email protected]}</li>

{Menu Data= $entry level= $level +1}

{Else}

<li>{$entry}</li>

{/if}

{/foreach}

</ul>

{/function}

---

{$menu = [' item1 ', ' item2 ', ' item3 ' =>[' item3-1 ', ' item3-2 ', ' item3-3 ' = [' item3-3-1 ', ' item3-3-2 ']], ' ITEM4 '}

{Menu Data= $menu}

Output:

* ITEM1

* ITEM2

* ITEM3

o item3-1

o item3-2

o item3-3

+ item3-3-1

+ item3-3-2

* ITEM4

three. Template Inheritance

Blocks can now be defined in the parent template, and blocks can be used or modified in a child template, such as:

PARENT.TPL:

CHILD.TPL:

{extends file= "PARENT.TPL"} {block name= "title"} Child Title{/block}

GRANDCHILD.TPL:

{extends file= "CHILD.TPL"} {block name= "title"} home-{$smarty. Block.parent}{/block}{block name= "Page-title"}my home{/block}{block name= "content"}{foreach $images As $img}{/foreach}{/block}

GRANDCHILD.TPL renders it this way:

A few points to note:

1. The sub-template can only have {extends} and {block}, other tags will be ignored;

2. Smarty3 supports infinite inheritance, but it is obvious that deeper inheritance means more overhead;

3. In addition to supporting the extends parent template in the sub-template, you can also write in PHP:

$smarty->display (' Extends:parent.tpl|child.tpl|grandchild.tpl ');

4. The block tag also supports append, prepended two options, which indicate append and prepend append, such as:

{block name= ' title ' Append} My Title {/block}

Four. Other

Smarty3 also supports many other new features, which are not covered here first. such as plugin, PHP Templates, varialbe scope, and variable Storage.

Five. Summary

Smarty3 has done a lot of usability upgrades, the new syntax can greatly provide development efficiency, the new template inheritance is also conducive to our better planning template structure, performance, according to official instructions to improve the 200%~300%. Because it is still RC version, the stability and performance aspects need to be further observed. It is recommended that we keep a close eye on it and raise it when it is appropriate.

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.