Smarty 用習慣了,還是會想使用 Smarty 來當樣版
所以就找了一下該怎麼將 Smarty 放進 Zend Framework
找到一篇寫的不錯的文章,簡單易懂:
Zend Framework: Using Smarty as template engine
那我這裡也來說明一下~~
有點小改,因為我是使用ZF 1.8,但差別不大,只差在Registry
這裡寫一下我的做法..
首先在 index.php 建立 smarty object
require_once('Zend/Registry.php');
require_once('Smarty/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->debugging = false;
$smarty->force_compile = true;
$smarty->caching = false;
$smarty->compile_check = true;
$smarty->cache_lifetime = -1;
$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
Zend_Registry::set('smarty', $smarty);
然後只要在controller class裡,取回smarty object,然後就照原本的寫法,就可以使用了。
class IndexController extends Zend_Controller_Action
{
  function indexAction()
  {
    $smarty = Zend_Registry::get('smarty');
    $smarty->assign('title', 'Test');
    $smarty->display('bala.tpl');
  }
}
夠簡單吧~~
 
0 comments:
張貼留言