เกริ่นนำ

ทำไมต้องใช้ Smarty ทำไมไม่ใช้ Zend_View ไปเลย??

นั่นหน่ะสิครับ ... โดยส่วนตัว ผมก็ไม่กล้าฟันธงน้ะครับว่าทำไม เพราะอะไร ผมคิดว่า ก็แล้วแต่ความชอบ และความเหมาะสมกับงานของใครของมัน ก็แล้วกัน :)

สำหรับผม เหตุผลง่ายๆ ดังนี้ครับ::

  • ผมยังไม่ได้ศึกษาการใช้งาน Zend_View อย่างจริงจัง
  • ผมว่า Smarty Template เป็นมิตรกับ Designer มากกว่า Zend_View
  • ที่สำคัญ ผมชอบ Smarty!
  • อย่างไรก็ดี Zend_View ก็เก่งมากตัวนึง มีอะไรให้ใช้เยอะแยะ เยอะขึ้นไปเรื่อยๆ และจะน่าตื่นเต้นมาก เมื่อเอา Java Script Framework เจ้าดังเจ้าหนึ่ง อย่าง Dojo เข้าไปรวมใน Zend_View (บทความเก่าของ pomcob) แต่ตอนนี้ไม่ได้ตามต่อเลย ไม่รู้ตอนนี้เขาทำไปถึงไหนแล้ว
  • ไม่ช้าก็เร็ว ผมว่าผมก็คงต้องเปลี่ยนใจไปใช้ Zend_View ซักวัน แต่โปรเจ็คนี้ ขอ Smarty สุดที่รักไปก่อน มันรีบ ... :D

 

การเอา Smarty มาใช่ร่วมกับ Zend Framework

ในการนำ Smarty มาใช้ร่วมกับ Zend Framework นั้น จะเป็นการใช้ Smarty มาช่วยจัดการส่วน Presentation Layers หรือ Views ใน MVC วิธีการนั้น ก็มีบางคนแนะนำให้ Extend Zend_View มาแล้วใส่ Smarty เข้าไปใน Zend_View แต่บางคนก็บอกว่า ถ้าจะไม่ใช้ Zend_View ก็ไม่น่าจะต้อง Extend มาให้เปลืองเปล่าๆ ไหนๆ จะใช้ Smarty อยู่แล้วก็ Disable Zend_View ไปเลย ดีกว่า (บทความเก่า) และที่ผมจะนำเสนอนี้ ก็เป็นแบบหลังนี้น้ะครับ เพราะผมก็เห็นดีด้วยว่า ถ้าไหนๆ จะไม่ใช้ ก็ไม่อยากจะไป Extend ... มาดูกันเลยดีว่า ว่าทำยังงัย ไม่ยากครับ...

 

Bootstrap

ใน Bootstrap ส่วนใหญ่ก็ไม่มีอะไรต่างไปจากเดิม มีเพียงเพิ่มเซต class path ไปหา Smarty lib files และมีการ require Smarty เท่านั้นเอง

$docsRoot =  getenv("DOCUMENT_ROOT");
$docsRoot = ereg_replace('\/$', '', $docsRoot) . "/zf-smarty/";
define('DOCUMENT_ROOT', $docsRoot);
 
set_include_path(DOCUMENT_ROOT
    . PATH_SEPARATOR . DOCUMENT_ROOT . 'libs/'
    . PATH_SEPARATOR . DOCUMENT_ROOT . 'libs/Business/'
    . PATH_SEPARATOR . DOCUMENT_ROOT . 'libs/Smarty/'
    . PATH_SEPARATOR . DOCUMENT_ROOT . 'applications/models/'
    . PATH_SEPARATOR . DOCUMENT_ROOT . 'applications/controllers/'
    . PATH_SEPARATOR . get_include_path()
);
 
require "Smarty.class.php";

 

เอา Smarty มาใส่ตรงไหน?

การนำ Smarty ใช้ก็ค่อนข้างตรงไป ตรงมา ประเด็นมันก็คือ การสร้าง Smarty Object ขึ้นมา แล้วทำให้ app. ของเรารู้จัก ใน action ต่างๆ แล้วก็เรียกใช้งาน Smarty เพื่อ assign() ข้อมูลให้ และเรียก display() ตามปกติ อย่างที่เราคุ้นเคย

ใน Zend_Controller_Action จะมี method init() ซึ่งจะถูกเรียกใช้งานเป็นอันดับแรก ทุกครั้งที่ Controller ของเรา ถูกเรียกให้ทำงาน ผมก็เลย สร้าง Smarty Object ไว้ใน method init() นี้ แต่ว่าจะสร้าง class กลางตัวหนึ่ง เป็น Base ไว้ให้ Controller อื่นๆ มา extend ต่อ ... พูดไปเยอะ ผมก็งงเอง มาดู code เลยดีกว่า จะได้ไม่งง

<?php
//
// File: controllers/MyBaseControllerAction.php
//
class MyBaseControllerAction extends Zend_Controller_Action
{
    protected $_config;
    protected $_smarty;
    protected $_lang;
    protected $_db;
 
    /**
    *   Initial method
    */
    public function init()
    {
        // load configuration
        $this->_config = new Zend_Config_Xml(DOCUMENT_ROOT . "config.xml", "general");
        // Zend_Registry::set("config", $this->_config);
 
        // setup database
        $this->_db = Zend_Db::factory($this->_config->database->adapter, $this->_config->database->toArray());
        $this->_db->query("SET NAMES UTF8");
        $this->_db->setFetchMode(Zend_Db::FETCH_ASSOC);
        Zend_Db_Table::setDefaultAdapter($this->_db);
 
        // pre-define smarty object
        $smarty = new Smarty;
        $smarty->debugging      = false;
        $smarty->force_compile  = true;
        $smarty->caching        = false;
        $smarty->compile_check  = true;
        $smarty->cache_lifetime = -1;
        $smarty->template_dir   = DOCUMENT_ROOT . 'themes/' . $this->_config->defaultTheme . '/templates';
        $smarty->compile_dir    = DOCUMENT_ROOT . 'themes/' . $this->_config->defaultTheme . '/templates_c';
        $smarty->config_dir     = DOCUMENT_ROOT . 'languages/';
        $smarty->plugins_dir    = array(SMARTY_DIR . 'plugins', './libs/SmartyPlugins');
 
        // Zend_Registry::set('smarty', $this->smarty);
        $this->_smarty =& $smarty;
 
        $this->_smarty->assign('lang',           $this->_config->defaultLanguage);
        $this->_smarty->assign('basePath',       $this->_request->getBasePath());
 
        $reqParams = $this->_request->getParams();
        if( $reqParams ) foreach( $reqParams as $k => $v)
        {
            $this->_smarty->assign($k, $v);
        }
    }
}
?>
แล้ว Controller ที่ทำงาน ก็มา extend Base Controller ตัวนี้ ดังนี้
<?php
//
// File: controllers/IndexController.php
//
class IndexController extends MyBaseControllerAction
{
    /**
    *   Default Index Action
    */
    function indexAction()
    {
        $this->_smarty->display("index/index.html");
    }
}
?>
เท่านี้เอง ก็ได้ใช้ Smarty สมใจแล้วครับ หรือจะลองดาวน์โหลด ตัวอย่างไปดูก็ได้ครับ ตัวอย่างชุดนี้ คิดว่า คงจะไม่มีปัญหาเยอะ เหมือนตัวที่แล้ว :)

 

Online Demo

http://lab.phpzealots.com/zf-smarty

AttachmentSize
zf-smarty.rar350.96 KB