ขอเกริ่นนำนิดหน่อยน้ะครับ ดังจะทราบกันดีอยู่แล้ว (หรือถ้าไม่ทราบ ก็เปิด manual แล้วดูที่ Chapter 3. Basic Syntax) ว่า syntax ของ Smarty หรือ tag ของ smarty นั้น ต้องขึ้นต้นด้วย { และจบด้วย } เช่น

{assign var="myFirstMessage" value="Hwllo World!"}
<b>{$myFirstMessage}</b>

ซึ่งทาง smarty เองก็เปิดโอกาสให้เราเปลี่ยนไปใช้อะไรก็ได้ ตามต้องการ เช่น ไม่ชอบ {} แต่อยากเปลี่ยนเป็น [] ก็ย่อมได้ ตามใจชอบ แต่ต้องให้มีคู่ เปิด [ และปิด ] ที่เหมาะสม และควรจะเป็นเท่านั้นเอง (คงไม่มีใครใช้ เปิด [ และปิดด้วย [ เหมือนกันน้ะ และคงไม่มีใครใช้ <> น้ะ เพราะว่ามันซ้ำกับ tag ของ HTML) ซึ่งทำได้โดย config smarty เพิ่มเติมนิดหน่อย ดังนี้

$smarty = new Smarty;
$smarty->left_delimiter  = "[";
$smarty->right_delimiter = "]";

แต่เมื่อเราจะใช้ tag ของ smarty นี้ในส่วนของ template file (แน่นอนว่า เราต้องใช้ smarty ใน template เพราะ smarty เกิดมา เพื่อจัดการส่วนของ view หรือ presentation layer ใน MVC) ซึ่งมักจะเป็น HTML, WML หรือ XHTML  และเมื่อเรา view ดูการแสดงผลของ template เช่นกด Ctrl+B ใน EditPlus หรือในโหมด design ใน Dreamweaver เราก็จะเห็น tag ของ smarty เต็มไปหมด เพราะแน่นอนว่า HTML จะมอง tag ของ smarty เป็น text ธรรมดาดีๆนี่เอง เช่น

// myCustomerListTpl.html code#1
<html>
<head>
<title>My Customers List</title>
</head>
<body>
{* my customers list *}
<table border="1">
<tr>
    <td>ID</td>
    <td>Full Name</td>
    <td>Phone No.</td>
</tr>
{foreach from=$customers item=theCustomer}
<tr>
    <td>{$theCustomer.id}</td>
    <td>{$theCustomer.fullName}</td>
    <td>{$theCustomer.phone}</td>
</tr>
{/foreach}
</table>
</body>
</html>

// myCustomerListTpl.html output with design view#1

{* my customers list *}

{foreach from=$customers item=theCustomer}

{/foreach}

ID Full Name Phone No.
{$theCustomer.id} {$theCustomer.fullName} {$theCustomer.phone}

จะเห็นได้ว่า "myCustomerListTpl.html output with design view" นั้นจะมี tag ของ smarty โผล่มา ซึ่งใน design view เราไม่ต้องการ แต่ก็มีวิธีแก้ไขง่ายๆ ซึ่งคงไม่มีวิธีอื่น นอกจากให้ HTML มอง tag ของ smarty เป็น HTML comment

ทีนี้ ผมก็เลยคิดว่า ถ้ายังงั้น จะเปลี่ยน delimiter ของ smarty จาก {} ให้เป็น <!--[ และ ]--> ซะเลย ทุก tag ของ smarty จะได้เป็น HTML comment ทั้งหมด และไม่แสดงใน design view เช่น

// myCustomerListTpl.html code#2
<html>
<head>
<title>My Customers List</title>
</head>
<body>
<!--[* my customers list *]-->
<table border="1">
<tr>
    <td>ID</td>
    <td>Full Name</td>
    <td>Phone No.</td>
</tr>
<!--[foreach from=$customers item=theCustomer]-->
<tr>
    <td><!--[$theCustomer.id]--></td>
    <td><!--[$theCustomer.fullName]--></td>
    <td><!--[$theCustomer.phone]--></td>
</tr>
<!--[/foreach]-->
</table>
</body>
</html>

// myCustomerListTpl.html output with design view#2


ID Full Name Phone No.

 

จะเห็นได้ว่า ผลลัพธ์ไม่มี tag ของ smarty หลงเหลืออยู่เลย แต่! อะไรที่ควรแสดง มันก็ดันหายไปหมดเลย ซึ่งจริงๆ ถ้าเราเห็นใน design view อย่างนี้ น่าจะดีกว่า


ID Full Name Phone No.
$theCustomer.id $theCustomer.fullName $theCustomer.phone

 

ดังนี้แล้ว ผมก็กลับมาพิจารณา delimiter ที่ผมเปลี่ยนไปใหม่ ก็พบว่า ... นี่เราโง่เองนี่หว่า จริงๆ แล้วไม่ต้องไปปรับอะไรใน smarty config เลย แต่เรามาปรับการเขียน code ใน template ให้เหมาะสม เท่านั้นเอง ดังนี้

// myCustomerListTpl.html code#3 ใส่ HTML comment คลอบ tag smarty เท่าที่จำเป็น

<html>
<head>
<title>My Customers List</title>
</head>
<body>
<!--{* my customers list *}-->
<table border="1">
<tr>
    <td>ID</td>
    <td>Full Name</td>
    <td>Phone No.</td>
</tr>
<!--{foreach from=$customers item=theCustomer}-->
<tr>
    <td>{$theCustomer.id}</td>
    <td>{$theCustomer.fullName}</td>
    <td>{$theCustomer.phone}</td>
</tr>
<!--{/foreach}-->
</table>
</body>
</html>

// myCustomerListTpl.html output with design view#2

 


ID Full Name Phone No.
{$theCustomer.id} {$theCustomer.fullName} {$theCustomer.phone}

 

เท่านี้เอง ก็ work work แล้ว ...