2. ดาว์นโหลด Smarty ในบทความนี้จะใช้เวอร์ชั่น 2.6.27 โหลด
3. แตกไฟล์ที่โหลดมาไว้ที่ htdocs/myhome
4. สร้างโฟลเดอร์ที่ต้องใช้กับ smarty
- myhome/templates
- myhome/templates_c
- myhome/configs
- myhome/cache
templates_c และ cache ต้องตั้ง permission 777
5. สร้างไฟล์แรก setup.php
โค้ด: เลือกทั้งหมด
<?php
// myhome/setup.php
require_once( "../../Smarty-2.6.27/libs/Smarty.class.php" ); // นำเข้า smarty class
class Smarty_MyHome extends Smarty
{
function Smarty_MyHome()
{
/* กำหนดพาธ 4 diretory ที่ต้องใช้ */
$this->Smarty();
$this->template_dir = dirname( __FILE__ ) . "/templates/";
$this->compile_dir = dirname( __FILE__ ) . "/templates_c/";
$this->config_dir = dirname( __FILE__ ) . "/configs/";
$this->cache_dir = dirname( __FILE__ ) . "/cache/";
$this->caching = true; // เปิดการใช้งาน cach
/* การส่งค่าไปแสดงใน layout */
$this->assign( "title", "ก้าวแรกสู่ Smarty 2.6.27" ); // การกำหนดค่าแบบ string
$arr["head"] = "My Home"; // การกำหนดค่าแบบ array
$arr["content"] = "Wellcome to my home.";
$this->assign( "arr", $arr );
}
}
?>
โค้ด: เลือกทั้งหมด
<?php
// myhome/htdocs/index.php
require( "../setup.php" ); // นำเข้าไฟล์ setup.php
$smarty = new Smarty_MyHome(); // สร้าง class
$smarty->assign( "name", "Ned" ); // การส่งค่าเพื่อนำไปใช้ใน layout
$smarty->display( "index.tpl" ); // เรียกใช้ layout
?>
โค้ด: เลือกทั้งหมด
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{$title} - Smarty</title> <!-- {$title} การแสดงค่าแบบ string ที่กำหนดมาจาก setup.php -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>{$arr.head}</h1> <!-- {$arr.head} การแสดงค่าแบบ array ที่กำหนดมาจาก setup.php -->
<div>{$arr.content}</div>
</body>
</html>
http://www.smarty.net/docsv2/en/languag ... iables.tpl