เราจะเรียกใช้งาน array ใน index.tpl เวลาที่เราใช้ smarty template ได้ยังไงหรอคะ
ทำได้โดย assign ค่า ตัวแปร array เข้าไปใน smarty แล้วฝั่ง template ก็ใช้ foreach วนลูปค่าใน array
ตัวอย่าง
File: testArrSmarty.php -------------------- <?php $myCustomerArr = array( array( 'id' => 1, 'name' => 'Mister 01' ), array( 'id' => 2, 'name' => 'Mister 02' ), array( 'id' => 3, 'name' => 'Mister 03' ), );
include "Smarty/libs/Smarty.class.php"; $smarty = new Smarty; $smarty->template_dir = 'templates'; $smarty->compile_dir = 'templates_c'; $smarty->assign('myCustomerArr', $myCustomerArr); $smarty->display('testArrSmarty.html'); ?>
File: templates/testArrSmarty.html -------------------- <html> <head> <title>Test Array in Smarty - phpzealots.com</title> <meta name="generator" content="editplus" /> </head>
<body> <h1>My Customers</h1> <table border="1"> <tr> <td align="center"><b>ID</b></td> <td align="center"><b>Name</b></td> </tr> <!--{foreach from=$myCustomerArr item=theCustomer}--> <tr> <td>{$theCustomer.id}</td> <td>{$theCustomer.name}</td> </tr> <!--{/foreach}--> </table> </body> </html>
หรือดูรายละเอียดได้ที่ :http://smarty.php.net/manual/en/language.function.foreach.php
นอกจากนี้ สามารถใช้ if-else เพื่อเช็คว่า array นั้นว่างหรือเปล่า ก่อนได้น้ะครับ เช่น
<!--{if !$myCustomerArr}--> <font color="#FF0000">No customer found!</font> <!--{else}--> <h1>My Customers</h1> <table border="1"> <tr> <td align="center"><b>ID</b></td> <td align="center"><b>Name</b></td> </tr> <!--{foreach from=$myCustomerArr item=theCustomer}--> <tr> <td>{$theCustomer.id}</td> <td>{$theCustomer.name}</td> </tr> <!--{/foreach}--> </table> <!-- {/if}-->
เพิ่มเติมอีกหน่อย
ใน smarty เวลาอ้างถึง element ใน array จะใช้จุด (.) ตามด้วยชื่อ key เช่น
PHP - $myArray[id] Smarty - $myArray.id
ทำได้โดย assign ค่า ตัวแปร array เข้าไปใน smarty แล้วฝั่ง template ก็ใช้ foreach วนลูปค่าใน array
ตัวอย่าง
File: testArrSmarty.php
--------------------
<?php
$myCustomerArr = array(
array(
'id' => 1,
'name' => 'Mister 01'
),
array(
'id' => 2,
'name' => 'Mister 02'
),
array(
'id' => 3,
'name' => 'Mister 03'
),
);
include "Smarty/libs/Smarty.class.php";
$smarty = new Smarty;
$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->assign('myCustomerArr', $myCustomerArr);
$smarty->display('testArrSmarty.html');
?>
File: templates/testArrSmarty.html
--------------------
<html>
<head>
<title>Test Array in Smarty - phpzealots.com</title>
<meta name="generator" content="editplus" />
</head>
<body>
<h1>My Customers</h1>
<table border="1">
<tr>
<td align="center"><b>ID</b></td>
<td align="center"><b>Name</b></td>
</tr>
<!--{foreach from=$myCustomerArr item=theCustomer}-->
<tr>
<td>{$theCustomer.id}</td>
<td>{$theCustomer.name}</td>
</tr>
<!--{/foreach}-->
</table>
</body>
</html>
หรือดูรายละเอียดได้ที่ :http://smarty.php.net/manual/en/language.function.foreach.php
นอกจากนี้ สามารถใช้ if-else เพื่อเช็คว่า array นั้นว่างหรือเปล่า ก่อนได้น้ะครับ เช่น
<!--{if !$myCustomerArr}-->
<font color="#FF0000">No customer found!</font>
<!--{else}-->
<h1>My Customers</h1>
<table border="1">
<tr>
<td align="center"><b>ID</b></td>
<td align="center"><b>Name</b></td>
</tr>
<!--{foreach from=$myCustomerArr item=theCustomer}-->
<tr>
<td>{$theCustomer.id}</td>
<td>{$theCustomer.name}</td>
</tr>
<!--{/foreach}-->
</table>
<!-- {/if}-->
เพิ่มเติมอีกหน่อย
ใน smarty เวลาอ้างถึง element ใน array จะใช้จุด (.) ตามด้วยชื่อ key เช่น
PHP - $myArray[id]
Smarty - $myArray.id