Great Templates

FEATURED WEB TEMPLATES

Amazon Books

Learn PHP

PHP Training
Zend Cert Training Zend Certified Engineer Zend PHP Pro

Smarty: Built In Functions-section,sectionelse

bsection,sectionelse

Attribute Name Type Required Default Description
name string Yes n/a The name of the section
loop mixed Yes n/a Value to determine the number of loop iterations
start integer No 0

The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.

step integer No 1

The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards.

max integer No n/a Sets the maximum number of times the section will loop.
show boolean No true determines whether or not to show this section

Template sections are used for looping over arrays of data. All section tags must be paired with /section tags. Required parameters are name and loop. The name of the section can be anything you like, 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. When printing a variable within a section, the section name must be given next to variable name within brackets []. sectionelse is executed when there are no values in the loop variable.

Example 7-15. section
{* this example will print out all the values of the $custid array *}
{section name=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 within the section.
This example assumes that $custid, $name and $address are all
arrays containing the same number of values *}
{section name=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 names
{* the name of the section can be anything you like,
and it is used to reference the data within the section *}
{section name=mydata loop=$custid}
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 name=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@myexample.com


id: 1001

name: Jack Jones

address: 417 Mulberry ln

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jack@myexample.com


id: 1002

name: Jane Munson

address: 5605 apple st

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jane@myexample.com

 

Example 7-19. sections and associative arrays
{* This is an example of printing an associative array
of data within 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@myexample.com


name: Jack Jones

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jack@myexample.com


name: Jane Munson

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jane@myexample.com

 

Example 7-20. sectionelse
{* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
id: {$custid[customer]}

{sectionelse}
there are no values in $custid.
{/section}

Sections also have their own variables that handle section properties. These 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.

index

index is used to display the current loop index, starting with zero (or the start attribute if given), and incrementing by one (or by the step attribute if given.)

Technical Note: If the step and start section properties are not modified, then this works the same as the iteration section property, except it starts on 0 instead of 1.

Example 7-21. section property index
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}

{/section}


OUTPUT:

0 id: 1000

1 id: 1001

2 id: 1002

index_prev

index_prev is used to display the previous loop index. on the first loop, this is set to -1.

Example 7-22. section property index_prev
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}

{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
{if $custid[customer.index_prev] ne $custid[customer.index]}
The customer id changed

{/if}
{/section}


OUTPUT:

0 id: 1000

The customer id changed

1 id: 1001

The customer id changed

2 id: 1002

The customer id changed

index_next

index_next is used to display the next loop index. On the last loop, this is still one more than the current index (respecting the setting of the step attribute, if given.)

Example 7-23. section property index_next
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}

{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
{if $custid[customer.index_next] ne $custid[customer.index]}
The customer id will change

{/if}
{/section}


OUTPUT:

0 id: 1000

The customer id will change

1 id: 1001

The customer id will change

2 id: 1002

The customer id will change

iteration

iteration is used to display the current loop iteration.

Note: This is not affected by the section properties start, step and max, unlike the index property. Iteration also starts with 1 instead of 0 like index. rownum is an alias to iteration, they work identical.

Example 7-24. section property iteration
{section name=customer loop=$custid start=5 step=2}
current loop iteration: {$smarty.section.customer.iteration}

{$smarty.section.customer.index} id: {$custid[customer]}

{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
{if $custid[customer.index_next] ne $custid[customer.index]}
The customer id will change

{/if}
{/section}


OUTPUT:

current loop iteration: 1
5 id: 1000

The customer id will change

current loop iteration: 2
7 id: 1001

The customer id will change

current loop iteration: 3
9 id: 1002

The customer id will change

first

first is set to true if the current section iteration is the first one.

Example 7-25. section property first
{section name=customer loop=$custid}
{if $smarty.section.customer.first}

{/if}



{if $smarty.section.customer.last}
{$smarty.section.customer.index} id:
{$custid[customer]}

{/if}
{/section}


OUTPUT:





0 id: 1000
1 id: 1001
2 id: 1002

last

last is set to true if the current section iteration is the last one.

Example 7-26. section property last
{section name=customer loop=$custid}
{if $smarty.section.customer.first}

{/if}



{if $smarty.section.customer.last}
{$smarty.section.customer.index} id:
{$custid[customer]}

{/if}
{/section}


OUTPUT:





0 id: 1000
1 id: 1001
2 id: 1002

rownum

rownum is used to display the current loop iteration, starting with one. It is an alias to iteration, they work identically.

Example 7-27. section property rownum
{section name=customer loop=$custid}
{$smarty.section.customer.rownum} id: {$custid[customer]}

{/section}


OUTPUT:

1 id: 1000

2 id: 1001

3 id: 1002

loop

loop is used to display the last index number that this section looped. This can be used inside or after the section.

Example 7-28. section property index
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}

{/section}

There were {$smarty.section.customer.loop} customers shown above.

OUTPUT:

0 id: 1000

1 id: 1001

2 id: 1002


There were 3 customers shown above.

show

show is used as a parameter to section. show is a boolean value, true or false. If false, the section will not be displayed. If there is a sectionelse present, that will be alternately displayed.

Example 7-29. section attribute show
{* $show_customer_info may have been passed from the PHP
application, to regulate whether or not this section shows *}
{section name=customer loop=$custid show=$show_customer_info}
{$smarty.section.customer.rownum} id: {$custid[customer]}

{/section}

{if $smarty.section.customer.show}
the section was shown.
{else}
the section was not shown.
{/if}


OUTPUT:

1 id: 1000

2 id: 1001

3 id: 1002


the section was shown.

total

total is used to display the number of iterations that this section will loop. This can be used inside or after the section.

Example 7-30. section property total
{section name=customer loop=$custid step=2}
{$smarty.section.customer.index} id: {$custid[customer]}

{/section}

There were {$smarty.section.customer.total} customers shown above.

OUTPUT:

0 id: 1000

2 id: 1001

4 id: 1002


There were 3 customers shown above.

strip

Many times web designers run into the issue where white space and carriage returns affect the output of the rendered HTML (browser "features"), so you must run all your tags together in the template to get the desired results. This usually ends up in unreadable or unmanageable templates.

Anything within {strip}{/strip} tags in Smarty are stripped of the extra spaces or carriage returns at the beginnings and ends of the lines before they are displayed. This way you can keep your templates readable, and not worry about extra white space causing problems.

Technical Note: {strip}{/strip} does not affect the contents of template variables. See the strip modifier function.

Example 7-31. strip tags
{* the following will be all run into one line upon output *}
{strip}






This is a test


{/strip}


OUTPUT:

This is a test

Notice that in the above example, all the lines begin and end with HTML tags. Be aware that all the lines are run together. If you have plain text at the beginning or end of any line, they will be run together, and may not be desired results.


 


Learn PHP | Zend Certified Engineer | Zend PHP Pro | PHP Web Apps | Web Hosting Service | Low Cost Domain Names | Great Templates | Great Books | Testimonials | Tech.Articles | TOS | AUS | Home | Linux Apache MySQL PHP