Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

{section},{sectionelse}

{section}鍙互寰幆閬嶅巻 杩炵画鏁板瓧绱㈠紩鐨勬暟缁, 鍖哄埆浜{foreach} 鍙互寰幆浠绘剰鍏宠仈鏁扮粍. 姣忎釜{section}鏍囩閮藉繀椤绘湁涓涓尮閰嶇殑{/section}鍏抽棴鏍囩銆

Note

{foreach} 鍙互鍋氬埌浠讳綍{section}鍋氬埌鐨勫姛鑳, 鑰屼笖鏇寸畝鍗曞拰鏈夋洿娓呮櫚鐨勮娉曘備竴鑸洿鎺ㄨ崘浣跨敤{foreach}璇硶.

Note

{section}涓嶈兘鐢ㄤ簬寰幆鍏宠仈鏁扮粍锛屽畠浠呰兘寰幆鏁板瓧绱㈠紩鐨勩佽繛缁笅鏍囩殑 (0,1,2,...)鏁扮粍銆 瑕佸惊鐜叧鑱旀暟缁勶紝璇蜂娇鐢{foreach}

鍙傛暟鍚嶇О 绫诲瀷 蹇呴夊弬鏁 榛樿鍊 璇存槑
name string Yes n/a section鐨勫悕绉
loop mixed Yes n/a 鐢ㄤ簬寰幆鐨勫
start integer No 0 璁剧疆寮濮嬪惊鐜殑涓嬫爣銆傚鏋滆缃垚璐熷硷紝鍒欎細浠庢暟缁勭殑缁撴潫浣嶇疆寮濮嬨 姣斿璇达紝濡傛灉鏁扮粍涓湁7涓厓绱狅紝璁剧疆璇ュ间负-2锛屽垯寰幆灏嗕粠涓嬫爣5寮濮嬨 璁剧疆浜嗕笉姝g‘鐨勫硷紙姣斿璇村湪鏁扮粍闀垮害浠ュ鐨勫硷級閭d箞浼氳嚜鍔ㄨ绠椾负鏈鎺ヨ繎鐨勫笺
step integer No 1 寰幆鐨勬闀垮笺傛瘮濡傦紝step=2灏嗗惊鐜笅鏍0,2,4,绛夈 濡傛灉step鍊艰缃垚璐熸暟锛岄偅涔堝皢浠庢渶鍚庡紑濮嬭绠楁闀裤
max integer No n/a 璁剧疆鏈澶х殑寰幆娆℃暟銆
show boolean No TRUE 鏄惁鏄剧ず寰幆鍐呭

鍙夋爣璁:

鍚嶇О 璇存槑
nocache 鍏抽棴{section}缂撳瓨
  • nameloop鏄繀椤荤殑鍙傛暟銆

  • {section}name鍙互鏄换鎰忓瓧绗︼紝濡傛暟瀛椼佸瓧姣嶆垨涓嬪垝绾跨瓑锛屽拰 PHP 鍙橀噺涓鏍风殑鍛藉悕瑙勫垯銆

  • {section}鍙互宓屽锛岃屼笖宓屽鐨{section}鍚嶇О蹇呴』鍞竴銆

  • loop涓鑸槸鏁扮粍锛屽喅瀹氫簡{section}鐨勫惊鐜鏁般 鍚屾椂浣犱篃鍙互浼犻掍竴涓暣鏁版寚瀹氬惊鐜鏁般

  • 褰撳湪{section}鍐呮樉绀哄彉閲忔椂, {section}name蹇呴』缁欏彉閲忓悕绉板姞涓奫鏂规嫭鍙穄.

  • 濡傛灉loop灞炴т负绌猴紝{sectionelse}灏嗘墽琛屻

  • {section}鍚屾椂杩樻湁鑷繁鐨勫睘鎬с 杩欎簺灞炴ч兘鏄氳繃: {$smarty.section.name.property}鏉ヤ娇鐢紝鍏朵腑namename灞炴.

  • {section}鐨勫唴缃睘鎬ф湁锛 index, index_prev, index_next, iteration, first, last, rownum, loop, show, total.

Example聽7.63.聽绠鍗曠殑{section}渚嬪瓙

assign()璧嬪间竴涓暟缁


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

妯℃澘灏嗚緭鍑鸿鏁扮粍


{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
{section customer $custid} {* short-hand *}
  id: {$custid[customer]}<br />
{/section}
<hr />
{*  print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
{section foo $custid step=-1} {* short-hand *}
  {$custid[foo]}<br />
{/section}

  

杈撳嚭锛


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

  

Example聽7.64.聽{section}涓嶄娇鐢ㄨ祴鍊兼暟缁


{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}

杈撳嚭锛

  
10 12 14 16 18
<hr />
20 18 16 14 12 10

  

Example聽7.65.聽缁檣section}璁剧疆鍚嶇О

{section}name灞炴у彲浠ユ槸浠绘剰瀛楃锛岃鍙傝PHP 鍙橀噺瀹氫箟. 瀹冩槸鐢ㄤ簬寮曠敤{section}鐨勬暟鎹.


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

  

Example聽7.66.聽{section}涓娇鐢ㄥ叧鑱旀暟缁

涓嬮潰鏄娇鐢{section}鏉ヨ緭鍑哄叧鑱旀暟缁勭殑渚嬪瓙銆 杩欓噷鏄湪PHP浠g爜涓祴鍊$contacts 鏁扮粍鍒癝marty銆

  
<?php
$data = array(
          array('name' => 'John Smith', 'home' => '555-555-5555',
                'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
          array('name' => 'Jack Jones', 'home' => '777-555-5555',
                'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
          array('name' => 'Jane Munson', 'home' => '000-555-5555',
                'cell' => '123456', 'email' => 'jane@myexample.com')
        );
$smarty->assign('contacts',$data);
?>

  

璇ユā鏉跨敤浜庢樉绀$contacts


{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}

  

杈撳嚭锛


<p>
  name: John Smith<br />
  home: 555-555-5555<br />
  cell: 666-555-5555<br />
  e-mail: john@myexample.com
</p>
<p>
  name: Jack Jones<br />
  home phone: 777-555-5555<br />
  cell phone: 888-555-5555<br />
  e-mail: jack@myexample.com
</p>
<p>
  name: Jane Munson<br />
  home phone: 000-555-5555<br />
  cell phone: 123456<br />
  e-mail: jane@myexample.com
</p>

  

Example聽7.67.聽{section}鐨loop灞炴х殑婕旂ず渚嬪瓙

渚嬪瓙鍋囧畾$custid, $name$address涓変釜鏁扮粍涓搴旂殑鍊奸兘鏈夌潃鐩稿悓鐨勬暟瀛椾笅鏍囥 棣栧厛浠嶱HP浠g爜涓祴鍊煎埌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 apple st');
$smarty->assign('address',$addr);

?>

loop鍊间粎鏄寚瀹氬惊鐜殑娆℃暟銆 浣犲彲浠ュ湪{section}涓粰瀹冭缃换浣曠殑鍙橀噺銆 鍦ㄥ涓暟缁勫惊鐜椂姣旇緝鏈夌敤銆 浣犲彲浠ヤ紶閫掍竴涓暟缁勬潵璁╁叾璁$畻鎬绘暟鑰屾寚瀹氬惊鐜鏁帮紝涔熷彲浠ョ洿鎺ユ寚瀹氫竴涓惊鐜鏁扮殑鏁存暟銆


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

  

杈撳嚭锛


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

  

Example聽7.68.聽{section}宓屽

{section}鍙互宓屽浠绘剰鐨勬繁搴︺傞氳繃宓屽{section}浣犲彲浠ュ鐞嗗缁存暟缁勩 涓嬮潰鏄緥瀛愮殑.php鏂囦欢銆


<?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 N 45th', '417 Mulberry ln', '5605 apple 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', 'john@myexample.com'),
           array( '123-456-4', 'www.example.com'),
           array( '0457878')
        );
$smarty->assign('contact_info', $info);

?>
  

鍦ㄨ繖涓ā鏉块噷锛 $contact_type[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}

  

杈撳嚭锛


<hr>
  id: 1000<br />
  name: John Smith<br />
  address: 253 N 45th<br />
    home phone: 555-555-5555<br />
    cell phone: 666-555-5555<br />
    e-mail: john@myexample.com<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: Jane Munson<br />
  address: 5605 apple st<br />
    cell phone: 0457878<br />

  

Example聽7.69.聽{sectionelse}鐨勬暟鎹簱渚嬪瓙

鏁版嵁搴撴煡鎵剧殑缁撴灉(濡 ADODB 鎴 PEAR) 浼犻掑埌 Smarty

  
<?php
$sql = 'select id, name, home, cell, email from contacts '
      ."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
?>

妯℃澘灏嗕互琛ㄦ牸褰㈠紡鏄剧ず鏁版嵁缁撴灉


<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>
    <td>{$contacts[co].home}</td>
    <td>{$contacts[co].cell}</td>
    <td>{$contacts[co].email}</td>
  <tr>
{sectionelse}
  <tr><td colspan="5">No items found</td></tr>
{/section}
</table>


.index

index鏄綋鍓嶆暟缁勭殑绱㈠紩鍊硷紝浠0寮濮嬶紝鎴栬呬粠璁惧畾鐨start鍊煎紑濮嬨傚畠灏嗘瘡娆″惊鐜鍔1鎴栬呭鍔犳寚瀹氱殑step鍊笺

璇存槑

濡傛灉 stepstart閮芥病鏈夎鎸囧畾锛 閭d箞瀹冧細鍜iteration灞炴у緢鍍, 鍙笉杩囧畠鏄粠0寮濮嬶紝鑰宨teration鏄粠1寮濮.

Example聽7.70.聽{section} index 灞炴

璇存槑

$custid[customer.index]$custid[customer] 鏄竴鏍风殑.


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

  

杈撳嚭锛


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

   

.index_prev

index_prev涓婁竴娆″惊鐜殑绱㈠紩鍊笺 鍦ㄧ涓娆″惊鐜殑鏃跺欙紝瀹冩槸-1.

.index_next

index_next鏄笅涓娆″惊鐜殑绱㈠紩鍊笺 鍦ㄦ渶鍚庝竴娆″惊鐜椂锛屽畠浼氭瘮褰撳墠绱㈠紩鍔1锛屾垨鑰呭姞涓婃寚瀹氱殑step灞炴у笺

Example聽7.71.聽index, index_nextindex_prev 灞炴


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

鍦ㄨ〃鏍间腑鏄剧ず鏁扮粍


{* $rows[row.index] and $rows[row] are identical in meaning *}
<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>

  

杈撳嚭锛


index  id    index_prev prev_id index_next next_id
0      1001  -1	                1          1002
1      1002  0          1001    2          1003
2      1003  1          1002    3          1004
3      1004  2          1003    4          1005
4      1005  3          1004    5

   

.iteration

iteration鏄綋鍓嶇殑寰幆娆℃暟锛屼粠1寮濮嬨

Note

瀹冨拰index涓嶅悓锛屼笉浼氬彈鍒{section} 鐨勫睘鎬 start, stepmax绛夊奖鍝. 鑰屼笖鍜index涓嶅悓鐨勬槸锛iteration杩樻槸浠1寮濮嬭绠楃殑銆 rownumiteration鐨勫埆鍚,瀹冧滑鏄竴鏍风殑銆

Example聽7.72.聽iteration灞炴


<?php
// array of 3000 to 3015
$id = range(3000,3015);
$smarty->assign('arr',$id);
?>

妯℃澘灏嗘寜step=2鏉ユ樉绀$arr鐨勬暟缁勫厓绱


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

  

杈撳嚭锛


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 />

   

杩欓噷鏄彟涓涓緥瀛愶紝浣跨敤iteration灞炴ф潵鏄剧ず琛ㄦ牸锛 骞朵笖姣忎簲琛屾樉绀轰竴娆¤〃澶淬


<table>
{section name=co loop=$contacts}
  {if $smarty.section.co.iteration is div by 5}
    <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>

  

涓涓敤iteration灞炴ф潵浜ゆ浛鏄剧ず鏂囩珷姣忎笁琛岄鑹茬殑渚嬪瓙

  
  <table>
  {section name=co loop=$contacts}
    {if $smarty.section.co.iteration is even by 3}
      <span style="color: #ffffff">{$contacts[co].name}</span>
    {else}
      <span style="color: #dddddd">{$contacts[co].name}</span>
    {/if}
  {/section}
  </table>
  
    

Note

"is div by"璇硶鏄疨HP鍙栨ā杩愮畻鐨勪竴涓彉绉嶃傚彇妯¤繍绠{if $smarty.section.co.iteration % 5 == 1}涔熸槸鍙敤鐨勩

Note

浣犺繕鍙互鐢"is odd by"鏉ュ弽杞氦鏇裤

.first

濡傛灉褰撳墠鐨勫惊鐜槸绗竴娆★紝first灏嗚璁炬垚 TRUE

.last

濡傛灉褰撳墠鐨勫惊鐜槸鏈鍚庝竴娆★紝閭d箞last灏嗕负 TRUE

Example聽7.73.聽{section} 灞炴 firstlast

渚嬪瓙寰幆浜 $customers鏁扮粍锛屽湪寰幆鏈鍓嶉潰杈撳嚭澶撮儴鍖哄煙锛屽湪搴曠杈撳嚭搴曢儴鍖哄煙鐨勫唴瀹广 鍚屾椂涔熶娇鐢ㄤ簡 total 灞炴.


{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}

  

.rownum

rownum鏄綋鍓嶅惊鐜殑娆℃暟锛屼粠1寮濮嬨傚畠鏄iteration 鐨勫埆鍚嶃

.loop

loop 鏄渶鍚庝竴娆section}寰幆鐨勪笅鏍囥 瀹冨彲浠ュ湪{section}寰幆涓垨鑰呭惊鐜悗浣跨敤銆

Example聽7.74.聽{section} 灞炴 loop


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

  

杈撳嚭锛


0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
There are 3 customers shown above.

   

.show

show鏄竴涓竷灏斿煎弬鏁般傚鏋滆缃负FALSE锛宻ection灏嗕笉浼氳鏄剧ず銆 濡傛灉鏈{sectionelse}鏄剧ず锛屽畠浠皢琚氦鏇挎樉绀恒

Example聽7.75.聽show 灞炴

甯冨皵鍊 $show_customer_info 鍙互鍦≒HP绋嬪簭璧嬪煎苟浼犻掑埌妯℃澘涓紝 鍙互鎺у埗section鐨勬樉绀轰笌鍚︺


{section name=customer loop=$customers show=$show_customer_info}
  {$smarty.section.customer.rownum} id: {$customers[customer]}<br />
{/section}

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

  

杈撳嚭锛


1 id: 1000<br />
2 id: 1001<br />
3 id: 1002<br />

the section was shown.

   

.total

total{section}鐨勬绘暟銆 瀹冨彲浠ュ湪{section}寰幆涓垨鑰呭惊鐜悗浣跨敤銆

Example聽7.76.聽total渚嬪瓙


{section name=customer loop=$custid step=2}
  {$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
   There are {$smarty.section.customer.total} customers shown above.

  

鍙傝{foreach}, {for}, {while}$smarty.section.

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 15 plus 3? (Are you human?)

Advertisement