Smarty --- Section Used for loop

xiaoxiao2021-03-06  65

Section, SectionelseAttributename Type Required Default DescriptionName String Yes N / A THE Name of The Section

Loop [$ Variable_name] Yes N / A The Name of the Variable To Determine # of loop ity

Template Sections Are Used for looping over arrays of data. All section / section tags. Required parameters

Are Name and loop. The name of the section, name of the section, Made Up of letters, Numbers and underscores.

Sections Can Be Nested, And The Nested Section Names Must Be Unique from Each Other. The loop variable (Usually an Array of)

Values) DETERMINES The Number of Times The section Will Loop .hen Printing a Variable forin A Section, The Section Name

Must Be given next to variable name within brackets []. Sectionelse is Executed When there is no values ​​in the loop variable.

Example 7.15. Section {* This Example Will Print Out All The Values ​​of The $ CUSTID Array *}

{section = Customer loop = $ custid}

ID: {$ CustId [Customer]}

{/ section}

OUTPUT:

ID: 1000

ID: 1001

ID: 1002

Example 7.16. Section loop variable {* the loop variable only determines the number of times to loop.

You CAN Access Any Variable from The Template Withnin The Section.

THIS EXAMPLE Assumes That $ CUSTID, $ NAME AND $ Address Are ALL

Arrays Containing The Same Number of VALUES *}

{section = Customer loop = $ custid}

ID: {$ CustId [Customer]}

Name: {$ Name [Customer]}

Address: {$ Address [Customer]}

{/ section}

OUTPUT:

ID: 1000
Name: John Smith

Address: 253 N 45th

ID: 1001

Name: Jack Jones

Address: 417 Mulberry LN

ID: 1002

Name: Jane Munson

Address: 5605 Apple ST

Example 7.17. Section name {* the name of the section,

And it is buy to reason the data forin the section *}

{section name = mydata loop = $ cuptid}

ID: {$ Custid [mydata]}

Name: {$ Name [mydata]}

Address: {$ Address [mydata]}

{/ section}

Example 7.18. Nested Sections {* Sections Can Be Nested As Deep As You Like. With Nested Sections,

You Can Access Complex Data Structures, Such As Multi-Dimensional

Arrays. in this Example, $ Contact_Type [Customer] is an array of

Contact Types for the current customer. *}

{section = Customer loop = $ custid}

ID: {$ CustId [Customer]}

Name: {$ Name [Customer]}

Address: {$ Address [Customer]}

{section name = contact loop = $ contact_type [Customer]}

{$ Contact_Type [Customer] [Contact]}: {$ Contact_info [Customer] [Contact]}

{/ section}

{/ section}

OUTPUT:

ID: 1000

Name: John Smith

Address: 253 N 45th

Home Phone: 555-555-5555

Cell Phone: 555-555-5555

E-mail:

John@mydomain.com

ID: 1001

Name: Jack Jones

Address: 417 Mulberry LN

Home Phone: 555-555-5555

Cell Phone: 555-555-5555

E-mail:

Jack@mydomain.com

ID: 1002

Name: Jane Munson

Address: 5605 Apple ST

Home Phone: 555-555-5555

Cell Phone: 555-555-5555
E-mail:

Jane@mydomain.com

Example 7.19. Sections and associative arrays {* this is an esample of printing an associative array

Of data forin a section *}

{section name = Customer loop = $ Contacts}

Name: {$ Contacts [Customer] .Name}

Home: {$ Contacts [Customer] .home}

Cell: {$ Contacts [Customer] .Cell}

E-mail: {$ Contacts [Customer] .email}

{/ section}

OUTPUT:

Name: John Smith

Home: 555-555-5555

Cell: 555-555-5555

E-mail:

John@mydomain.com

Name: Jack Jones

Home Phone: 555-555-5555

Cell Phone: 555-555-5555

E-mail:

Jack@mydomain.com

Name: Jane Munson

Home Phone: 555-555-5555

Cell Phone: 555-555-5555

E-mail:

Jane@mydomain.com

Example 7.20. Sectionelse {* SectionsElse Will Execute if The are no $ cuptid value *}

{section = Customer loop = $ custid}

ID: {$ CustId [Customer]}

{sectionElse}

There Are No Values ​​In $ CustId.

{/ section}

Sections Also Have The Their Own Variables That Handle Section Properties. There Are Indicated Like So:

{$ smarty.section.sectionname.varname}

Note: as of smarty 1.5.0, The Syntax for Section Property Variables Has Been Changed from

{% sectionname.varname%} to {$ smarty.section.sectionname.varname}. The old syntax is still supported, but you

Will Only See Reference to The New Syntax in The Manual Examples.

转载请注明原文地址:https://www.9cbs.com/read-88758.html

New Post(0)