<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jQuery, Ajax, PHP, JSON, XML, Web Technologies &#187; Template Engine</title>
	<atom:link href="http://blog.chonla.com/category/template-engine/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.chonla.com</link>
	<description>คุยกันเรื่อง jQuery, Ajax, PHP, JSON, XML และ Web Technologies เป็นภาษาไทยกันดีกว่า</description>
	<lastBuildDate>Tue, 17 Jan 2012 18:27:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jTemplates, Template Engine ของ jQuery</title>
		<link>http://blog.chonla.com/2009/05/jtemplates-template-engine-%e0%b8%82%e0%b8%ad%e0%b8%87-jquery/</link>
		<comments>http://blog.chonla.com/2009/05/jtemplates-template-engine-%e0%b8%82%e0%b8%ad%e0%b8%87-jquery/#comments</comments>
		<pubDate>Tue, 19 May 2009 17:10:30 +0000</pubDate>
		<dc:creator>chonla</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Souce Code]]></category>
		<category><![CDATA[Template Engine]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jTemplate]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Template]]></category>

		<guid isPermaLink="false">http://blog.chonla.com/?p=149</guid>
		<description><![CDATA[ได้ไปอ่านบทความเกี่ยวกับ Template Engine ที่เว็บ jquerytips เป็น Template Engine ที่เป็น plugin ของ jQuery น่าสนใจดีครับ เลยเอามาแบ่งปันกัน jTemplate เป็น Template Engine ที่มีลักษณะการเขียน (Syntax) ที่เรียกได้ว่าลอกแบบกันมาจาก Smarty เลยทีเดียว ทำให้คนที่มีพื้นฐานของ Smarty อยู่แล้ว สามารถเรียนรู้ jTemplate ได้โดยง่าย ส่วนรูปแบบของข้อมูลที่รองรับก็จะอยู่ในรูปแบบของ JSON มาดูตัวอย่างการเขียนกันครับ ขั้นแรก ผมจะกำหนด data ที่จะใช้งานก่อนครับ var data = { id: 4, name: "User List", table: [ {id: 1, name: 'Anne', age: 22, mail: 'anne@domain.com'}, [...]]]></description>
			<content:encoded><![CDATA[<p>ได้ไปอ่านบทความเกี่ยวกับ Template Engine ที่เว็บ <a href="http://www.jquerytips.com" target="_blank">jquerytips</a> เป็น Template Engine ที่เป็น plugin ของ jQuery น่าสนใจดีครับ เลยเอามาแบ่งปันกัน<br />
<span id="more-149"></span><br />
jTemplate เป็น Template Engine ที่มีลักษณะการเขียน (Syntax) ที่เรียกได้ว่าลอกแบบกันมาจาก Smarty เลยทีเดียว ทำให้คนที่มีพื้นฐานของ Smarty อยู่แล้ว สามารถเรียนรู้ jTemplate ได้โดยง่าย ส่วนรูปแบบของข้อมูลที่รองรับก็จะอยู่ในรูปแบบของ JSON มาดูตัวอย่างการเขียนกันครับ</p>
<p>ขั้นแรก ผมจะกำหนด data ที่จะใช้งานก่อนครับ</p>
<pre class="js">var data = {
    id: 4,
    name: "User List",
    table: [
        {id: 1, name: 'Anne', age: 22, mail: 'anne@domain.com'},
        {id: 2, name: 'Amelie', age: 24, mail: 'amelie@domain.com'},
        {id: 3, name: 'Polly', age: 18, mail: 'polly@domain.com'},
        {id: 4, name: 'Alice', age: 26, mail: 'alice@domain.com'},
        {id: 5, name: 'Martha', age: 25, mail: 'martha@domain.com'}
    ],
    toString: function() {
        return this.name.bold() + ' (count: ' + this.table.length + ')';
    }
};</pre>
<p>เสร็จแล้วก็มากำหนด Template</p>
<pre class="js">{#template MAIN}
&lt;div id="header"&gt;{$T.name}&lt;/div&gt;
&lt;table&gt;
{#foreach $T.table as r}
    {#include row root=$T.r}
{#/for}
&lt;/table&gt;
{#/template MAIN}

{#template row}
&lt;tr bgcolor="{#cycle values=['#AAAAEE','#CCCCFF']}"&gt;
    &lt;td&gt;{$T.name.bold()}&lt;/td&gt;
    &lt;td&gt;{$T.age}&lt;/td&gt;
    &lt;td&gt;{$T.mail.link('mailto:'+$T.mail)}&lt;/td&gt;
&lt;/tr&gt;
{#/template row}</pre>
<p>ที่เหลือก็แค่เรียกใช้งาน plugin ตัวนี้เท่านั้นเองครับ ง่าย ๆ</p>
<pre class="js">$("#result").setTemplate($("textarea").text()).processTemplate(data);</pre>
<p>ลองไปดูตัวอย่างที่ <a title="LIVE DEMO : jTemplate for jQuery" href="http://sandbox.chonla.com/jtemplate" target="_blank">LIVE DEMO</a> ได้เลยครับ</p>
<p>แหล่งอ้างอิงและ API</p>
<ul>
<li><a href="http://jtemplates.tpython.com/">http://jtemplates.tpython.com/</a></li>
<li><a href="http://www.jquerytips.com/2008/12/07/jtemplate-template-engine-in-javascript/">http://www.jquerytips.com/2008/12/07/jtemplate-template-engine-in-javascript/</a></li>
</ul>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.chonla.com%2F2009%2F05%2Fjtemplates-template-engine-%25e0%25b8%2582%25e0%25b8%25ad%25e0%25b8%2587-jquery%2F&amp;title=jTemplates%2C%20Template%20Engine%20%E0%B8%82%E0%B8%AD%E0%B8%87%20jQuery" id="wpa2a_2"><img src="http://blog.chonla.com/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chonla.com/2009/05/jtemplates-template-engine-%e0%b8%82%e0%b8%ad%e0%b8%87-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ไอเดีย Template Engine แบบ Basic</title>
		<link>http://blog.chonla.com/2009/05/%e0%b9%84%e0%b8%ad%e0%b9%80%e0%b8%94%e0%b8%b5%e0%b8%a2-template-%e0%b9%81%e0%b8%9a%e0%b8%9a-basic/</link>
		<comments>http://blog.chonla.com/2009/05/%e0%b9%84%e0%b8%ad%e0%b9%80%e0%b8%94%e0%b8%b5%e0%b8%a2-template-%e0%b9%81%e0%b8%9a%e0%b8%9a-basic/#comments</comments>
		<pubDate>Thu, 14 May 2009 15:41:49 +0000</pubDate>
		<dc:creator>chonla</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Souce Code]]></category>
		<category><![CDATA[Template Engine]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Template]]></category>

		<guid isPermaLink="false">http://blog.chonla.com/?p=171</guid>
		<description><![CDATA[ใน framework ที่มีการแยก View ออกมาอย่างชัดเจน อย่างเช่น MVC จะมีการใช้ Template เข้ามาเป็นตัวช่วยในการช่วยแสดงผล ไม่ว่าจะเป็นการใช้ Template Engine อย่างเช่น smarty เป็นต้น ทีนี้ ผมก็มาคิด ๆ ดูว่าไอ้เจ้า Template เนี่ย มันมีไอเดียการทำงานยังไง ลองมาดูกัน หลักการง่าย ๆ ของการทำ Template คือ การแยกแนวคิดให้ตัว Template ทำหน้าที่แสดงผลอย่างเดียว โดยไม่มี Logic อื่น ๆ มาเกี่ยวข้อง เราอาจจะมี condition บางอย่างอยู่ใน template ได้ แต่ก็เฉพาะในส่วนที่เกี่ยวข้องกับการแสดงผลเท่านั้น ดังนั้น สิ่งที่จำเป็นก่อนการแสดงผลนั่นก็คือ การเตรียมข้อมูลที่จะแสดงให้พร้อมนั่นเอง ต่อจากนั้นเราก็นำข้อมูลที่เราเตรียมไว้ ไปยัดใส่ template ที่เราเตรียมไว้ เป็นอันเรียบร้อย เรามาสร้าง template engine แบบง่ายที่สุดในโลกกันดีกว่า function [...]]]></description>
			<content:encoded><![CDATA[<p>ใน framework ที่มีการแยก View ออกมาอย่างชัดเจน อย่างเช่น MVC จะมีการใช้ Template เข้ามาเป็นตัวช่วยในการช่วยแสดงผล ไม่ว่าจะเป็นการใช้ Template Engine อย่างเช่น <a title="Smarty Template Engine" href="http://www.smarty.net/" target="_blank">smarty</a> เป็นต้น</p>
<p>ทีนี้ ผมก็มาคิด ๆ ดูว่าไอ้เจ้า Template เนี่ย มันมีไอเดียการทำงานยังไง ลองมาดูกัน</p>
<p><span id="more-171"></span>หลักการง่าย ๆ ของการทำ Template คือ การแยกแนวคิดให้ตัว Template ทำหน้าที่แสดงผลอย่างเดียว โดยไม่มี Logic อื่น ๆ มาเกี่ยวข้อง เราอาจจะมี condition บางอย่างอยู่ใน template ได้ แต่ก็เฉพาะในส่วนที่เกี่ยวข้องกับการแสดงผลเท่านั้น ดังนั้น สิ่งที่จำเป็นก่อนการแสดงผลนั่นก็คือ การเตรียมข้อมูลที่จะแสดงให้พร้อมนั่นเอง ต่อจากนั้นเราก็นำข้อมูลที่เราเตรียมไว้ ไปยัดใส่ template ที่เราเตรียมไว้ เป็นอันเรียบร้อย</p>
<p>เรามาสร้าง template engine แบบง่ายที่สุดในโลกกันดีกว่า</p>
<pre name="code" class="js">
function apply_template($data, $template)
{
    ob_start();
    include ($template);
    $output = ob_get_clean();
    return $output;
} 
</pre>
<p>ดูเหมือนง่ายมั๊ยครับ แค่ไม่กี่บรรทัดเอง ส่วนของ template file เราก็จะมีตัวแปรไว้ใช้งานตัวนึง ชื่อว่า $data นั่นเองครับ เวลาเราส่งค่าเข้า template ก็ให้ส่งผ่านตัวแปรตัวเดียวครับ (อาจจะทำเป็น array หรือ object ก็ได้ครับ)</p>
<p>ลองดูตัวอย่างใน <a title="LIVE DEMO : Basic Template Engine" href="http://sandbox.chonla.com/template/" target="_blank">LIVE DEMO</a> นะครับ</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.chonla.com%2F2009%2F05%2F%25e0%25b9%2584%25e0%25b8%25ad%25e0%25b9%2580%25e0%25b8%2594%25e0%25b8%25b5%25e0%25b8%25a2-template-%25e0%25b9%2581%25e0%25b8%259a%25e0%25b8%259a-basic%2F&amp;title=%E0%B9%84%E0%B8%AD%E0%B9%80%E0%B8%94%E0%B8%B5%E0%B8%A2%20Template%20Engine%20%E0%B9%81%E0%B8%9A%E0%B8%9A%20Basic" id="wpa2a_4"><img src="http://blog.chonla.com/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chonla.com/2009/05/%e0%b9%84%e0%b8%ad%e0%b9%80%e0%b8%94%e0%b8%b5%e0%b8%a2-template-%e0%b9%81%e0%b8%9a%e0%b8%9a-basic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

