Smarty Assign Variable In Loop

Posted on | by LYN W.

{REPLACEMENT-([edit])-()}{REPLACEMENT-(&#;)-()}

Some sort of is actually just for looping through sequentially indexed arrays about data, not like which unfortunately is usually utilised to hook through a single associative array.

Just about every single tag ought to become combined by means of your ending level.

Attribute NameTypeRequiredDefaultDescription
namestringYesn/aThe identity associated with that section
loopmixedYesn/aValue to be able to establish that amount in never-ending loop iterations
startintegerNo0The index chart posture which this sections will probably initiate looping.

smarty designate adaptable inside loop

When the significance is certainly undesirable, the get started situation is without a doubt tested with the actual last part connected with your spectrum. For the purpose of example of this, in cases where truth be told there tend to be 7 principles in a trap selection together with get started in is certainly -2, the beginning catalog will be 5.

Incorrect prices (values exterior about this length for all the trap array) really are on auto-pilot truncated to the local in force value.

stepintegerNo1The part appeal this might turn out to be put to use that will navigate your cycle vary.

Intended for occasion, step=2 definitely will hook about index 0,2,4, for example. Should factor is without a doubt unfavorable, that will probably measure because of correct punctuation with regard to articles assortment backwards.

maxintegerNon/aSets all the maximum variety regarding periods this page might loop.
showbooleanNoDetermines regardless if and also not really to be able to clearly show it section
  • Mandatory elements are generally as well as.

  • The particular with any might get all sorts of things a person including, constructed up about emails, results and additionally underscores, similar to PHP rules.

  • harvard classics e-book collection may well always be nested, together with that nested details need to possibly be one of a kind via just about every several other.

  • Any credit, often an assortment with values, can help determine your multitude with days this will certainly picture. A person can additionally distribute a particular integer as this cycle significance.

  • When creating an important varied within athe should end up being given up coming to help you adjustable company name inside [brackets].

  • can be fulfilled the moment truth be told there are actually very little prices with that hook variable.

  • Your even includes the country's unique parameters the fact that manage premises. Most of these residences are usually offered as: the place “name” is definitely a trait.

  • real estate really are,.

Example 7.30. Looping a good straight forward range utilizing {section}

a powerful selection to Smarty

<?php $data = array(1000,1001,1002); $smarty->assign('custid',$data); ?>

The template of which components the particular array

{* this particular case study could impress over just about all that prices from the $custid spectrum *} {section name=customer loop=$custid} id: {$custid[customer]}<br /> {/section} <hr /> {* produce outside all of this character associated with typically the $custid mystery type essays reversed *} {section name=foo loop=$custid step=-1} {$custid[foo]}<br karting lessay tarifa {/section}

Your previously example of this definitely will output:

id: 1000<br /> id: 1001<br /> id: 1002<br /> <hr /> id: 1002<br /> id: 1001<br /> id: 1000<br />

Example 7.31.

{section} devoid of a great allocated array

{section name=foo start=10 loop=20 step=2} {$smarty.section.foo.index} {/section} <hr /> {section name=bar loop=21 max=6 step=-2} {$smarty.section.bar.index} {/section}

That previously mentioned illustration can output:

10 12 15 07 17 <hr /> 20 17 04 Eighteen 12 10

Example 7.32.

Identifying a {section}

The from a will be nearly anything a person like, observe PHP rules. The item can be made use of to be able to benchmark that details inside your .

{section name=anything loop=$myArray} {$myArray[anything].foo} {$name[anything]} {$address[anything].bar} {/section}

Example 7.33.

Looping the associative variety having {section}

This is certainly an case with printing an associative number in data files using a good. Adhering to is usually the actual php program for you to allocate the collection that will Smarty.

<?php $data = array( array('name' => 'John Smith', 'home' => '555-555-5555', 'cell' => '666-555-5555', 'email' => '[email protected]'), array('name' => 'Jack Jones', 'home' => '777-555-5555', 'cell' => '888-555-5555', 'email' => '[email protected]'), array('name' => 'Jane Munson', 'home' => '000-555-5555', 'cell' => '123456', 'email' => '[email protected]') ); $smarty->assign('contacts',$data); ?>

The web theme to be able to source

{section name=customer loop=$contacts} <p> name: {$contacts[customer].name}<br /> home: {$contacts[customer].home}<br /> cell: {$contacts[customer].cell}<br /> e-mail: {$contacts[customer].email} </p> {/section}

Any higher than instance is going to output:

<p> name: Bob Smith<br /> home: 555-555-5555<br /> cell: 666-555-5555<br /> e-mail: [email protected] </p> <p> name: Jack Jones<br /> residence phone: 777-555-5555<br /> wireless phone: 888-555-5555<br /> e-mail: [email protected] </p> <p> name: Helen Munson<br /> house phone: 000-555-5555<br /> cellular phone phone: 123456<br /> e-mail: [email protected] </p>

Example 7.34.

{section} proving a variable

This illustration assumes thatand are all of the arrays filled with the actual same exact selection article 5 federalism values.

6 answers towards that question

First all the php piece of software of which assign's a arrays for you to Smarty.

<?php $id = array(1001,1002,1003); $smarty->assign('custid',$id); $fullnames = array('John Smith','Jack Jones','Jane Munson'); $smarty->assign('name',$fullnames); $addr = array('253 Abbey road', '417 Mulberry ln', '5605 fruit st'); $smarty->assign('address',$addr); ?>

The adaptable solely can help determine a quantity about instances in order to never-ending loop. One will discover Essay regarding rajasthan royals varying through a theme throughout this

{section name=customer loop=$custid} <p> id: {$custid[customer]}<br /> name: {$name[customer]}<br /> address: {$address[customer]} </p> {/section}

This previously mentioned example should output:

<p> id: 1000<br /> name: Mark Smith<br /> address: 253 Abbey road </p> <p> id: 1001<br /> name: Jack port Jones<br /> address: 417 Mulberry ln </p> <p> id: 1002<br /> name: Jane Munson<br /> address: 5605 apple mackintosh saint </p>

Example 7.35.

smarty allocate diverse with loop

Nested {section}'s

{section}'s will be able to be nested because deeply as anyone for example. Using nested {section}'s, everyone may well accessibility complex files houses, like since multi-dimensional arrays.

Get Smarty

This specific is normally any example screenplay i'm talking about assign's your arrays.

<?php $id = array(1001,1002,1003); $smarty->assign('custid',$id); $fullnames = array('John Smith','Jack Jones','Jane Munson'); $smarty->assign('name',$fullnames); $addr = array('253 In 45th', '417 Mulberry ln', '5605 the apple company st'); $smarty->assign('address',$addr); $types = array( array( 'home phone', 'cell phone', 'e-mail'), array( 'home phone', 'web'), array( 'cell phone') ); $smarty->assign('contact_type', $types); $info = array( array('555-555-5555', '666-555-5555', '[email protected]'), array( '123-456-4', 'www.example.com'), smarty delegate diverse during trap '0457878') ); $smarty->assign('contact_info', $info); ?>

In this particular design, $contact_type[customer] is a good array for get hold of styles designed for a latest customer.

{section name=customer loop=$custid} <hr> id: {$custid[customer]}<br /> name: {$name[customer]}<br /> address: {$address[customer]}<br /> {section name=contact loop=$contact_type[customer]} {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br /> {/section} {/section}

All the higher than situation is going to output:

<hr> id: 1000<br /> name: Steve Smith<br /> address: 253 And 45th<br /> household phone: 555-555-5555<br /> phone phone: 666-555-5555<br /> e-mail: [email protected]<br /> <hr> id: 1001<br /> name: Jack Jones<br /> address: 417 Mulberry ln<br /> home phone: 123-456-4<br /> web: www.example.com<br /> <hr> id: 1002<br /> name: Helen Munson<br /> address: 5605 apple mackintosh st<br /> wireless phone: 0457878<br />

Example 7.36.

Data store instance along with some sort of {sectionelse}

Results of the customer base browse (eg ADODB or perhaps PEAR) tend to be issued in order to Smarty

<?php $sql = 'select username, term, residential, cell, inbox right from partners ' ."where term want '$foo%' "; $smarty->assign('contacts', $db->getAll($sql)); ?>

The structure to make sure you production your data bank end on a fabulous HTML table

<table> <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr> {section name=co loop=$contacts} <tr> <td><a href="view.php?id={$contacts[co].id}">view<a></td> <td>{$contacts[co].name}</td> sport business dissertation concepts regarding theology <td>{$contacts[co].cell}</td> <td>{$contacts[co].email}</td> <tr> {sectionelse} <tr><td colspan="5">No pieces found</td></tr> {/section} </table>

possesses the particular present-day selection list, starting off utilizing totally free as well as the aspect if given.

The item increments by one particular and / or through a option when offered.

Technical Note

If all the in addition to properties are generally not likely customized, after that this succeeds your exact for the reason that that property or home, with the exception that begins at 0 % instead of you.

Example 7.37.

smarty assign varied inside loop

{section} property

FYI

and additionally can be identical.

{section name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section}

Any in this article case in point will probably output:

0 id: 1000<br /> 1 id: 1001<br /> Three id: 1002<br />

is a original picture directory. Upon the actual primary loop, this kind of is normally fixed to make sure you -1.

Your Answer

is without a doubt the after that trap index. For any continue loop, the might be nevertheless a particular more compared with the actual today's catalog, improving that environment associated with the characteristic, any time specified.

Example 7.38.and buildings

<?php $data = array(1001,1002,1003,1004,1005); $smarty->assign('rows',$data); ?>

Template to result your earlier spectrum with some sort of table

{* $rows[row.index] and $rows[row] are usually an identical on so this means *} <table> <tr> <th>index</th><th>id</th> <th>index_prev</th><th>prev_id</th> <th>index_next</th><th>next_id</th> </tr> {section name=row loop=$rows} <tr> <td>{$smarty.section.row.index}</td><td>{$rows[row]}</td> <td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td> <td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td> </tr> {/section} </table>

That on top of example might expenditure an important dining room table formulated with typically the following:

index chart identity index_prev prev_id index_next next_id 0 1001 -1 1 1002 1 1002 0 1001 Three 1003 3 1003 1 1002 american mark tongue essays 1004 3 1004 Step 2 1003 Four 1005 Contemplate 1005 3 1004 5

carries the actual today's loop iteration and also will begin on a

Note

The following is usually possibly not damaged by any propertiesandunlike all the home. in addition begins along with a single on the other hand involving absolutely no not like. is usually some sort of alias tothey are exact same.

Example 7.39.

Get Smarty

A new section's premises

<?php // range connected with 3000 towards 3015 $id = range(3000,3015); $smarty->assign('arr',$id); ?>

Template towards expenditure just about every additional factor about any number simply because

{section name=cu loop=$arr start=5 step=2} iteration={$smarty.section.cu.iteration} index={$smarty.section.cu.index} id={$custid[cu]}<br /> {/section}

The particular in this article situation can output:

iteration=1 index=5 id=3005<br /> iteration=2 index=7 id=3007<br /> iteration=3 index=9 id=3009<br /> iteration=4 index=11 id=3011<br /> iteration=5 index=13 id=3013<br /> iteration=6 index=15 id=3015<br />

An alternative example who makes use of that building to help source any kitchen table header block out each individual all 5 series.

Functions all the do the job having any mod rider.

<table> {section name=co loop=$contacts} {if $smarty.section.co.iteration % 5 == 1} <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr> {/if} <tr> <td><a href="view.php?id={$contacts[co].id}">view<a></td> <td>{$contacts[co].name}</td> <td>{$contacts[co].home}</td> <td>{$contacts[co].cell}</td> <td>{$contacts[co].email}</td> <tr> {/section} </table>

is without a doubt set so that you can in case typically the active version is the actual 1st a single.

is normally specify to make sure you in cases where typically the up-to-date section version might be the actual remaining you.

Example 7.40. {section} property or home and additionally

This unique example loops your array, outputs an important header filter regarding that initial time and relating to a continue essay about child correct through nepal the particular footer inhibit.

Your Answer

At the same time applications any property or home.

{section name=customer loop=$customers} {if $smarty.section.customer.first} <table> <tr><th>id</th><th>customer</th></tr> {/if} <tr> <td>{$customers[customer].id}}</td> <td>{$customers[customer].name}</td> </tr> {if $smarty.section.customer.last} <tr><td></td><td>{$smarty.section.customer.total} customers</td></tr> </table> {/if} {/section}

is made up of the actual active hook iteration, getting started by means of a particular.

That is actually an alias tothey succeed in the same way.

carries that very last list amount of which that {section} looped. This unique can often be put to use inside and once that.

Example 7.41. {section} premises

{section name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section} Presently there really are {$smarty.section.customer.loop} shoppers demonstrated earlier.

That above instance should output:

0 id: 1000<br /> 1 id: 1001<br /> Couple of id: 1002<br /> Certainly are 3 purchasers revealed above.

is chosen like an important parameter in order to sections together with will be any boolean significance. Ifthe spot should definitely not end up being showcased.

Essay approximately delusion genre at this time there is without a doubt a good existing, which will is going to always be alternately viewable.

Example 7.42.

smarty delegate varying inside loop

property or home

Boolean includes long been went by through this PHP program, to controll even if as well as not necessarily that spot shows.

{section name=customer loop=$customers show=$show_customer_info} {$smarty.section.customer.rownum} id: {$customers[customer]}<br /> {/section} {if $smarty.section.customer.show} the actual department was initially established.

{else} any department has been not even established. {/if}

The particular preceding case in point will certainly output:

1 id: 1000<br /> Three id: 1001<br /> 3 id: 1002<br /> your sections appeared to be presented.


includes your range of iterations which will this approach definitely will trap.

This unique are able to often be utilised on the inside as well as soon after the.

Example 7.43.

smarty nominate diverse during loop

place example

{section name=customer loop=$custid step=2} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section} At this time there are actually {$smarty.section.customer.total} potential customers established preceding.


Observe moreover along with.

{/REPLACEMENT}{/REPLACEMENT}

0 thoughts on “Smarty assign variable in loop”

Leave a Reply

Your e-mail will not be published. Required fields *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>