Zend Framework整合Smarty方法[转]

Zend FrameworkPHP官方出的开发框架,随着PHP5的推广,越来越多的人开始学习、使用 Zend Framework。

Smarty是一个非常强大的模版引擎,由于其完善的语法、内置的缓存功能,已经被众多开发人员应用在自己的开发项目中。

正因为二者的强大,因此有越来越多的人开始考虑将Smarty集成到Zend Framework,以便充分的发挥各自的优点。
index.php入口文件:

<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Shanghai');
define('WEB_ROOT', 'http://localhost/zendframework/');//为分页设置的变量,pagestyle.phtml文件要用
set_include_path('.' .PATH_SEPARATOR .'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR .

get_include_path());

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件

//配制smarty参数
include "./public/scripts/Smarty/Smarty.class.php";//引入smarty文件
$smarty = new Smarty();
$smarty -> template_dir = "./application/views/scripts/";//设置模板显示路径
$smarty -> compile_dir = "./application/views/scripts/templates_c";
$smarty -> left_delimiter = "<!--{";
$smarty -> right_delimiter = "}-->";
$registry = Zend_Registry::getInstance();
$registry->set('smarty',$smarty);

//配置数据库参数,并连接数据库
$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query("SET NAMES {$config->general->db->config->charset}");
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
Zend_Registry::set('dbprefix',$config->general->db->config->prefix);
//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/zendframework')//设置基本路径
->setParam('noViewRenderer', true)
->setControllerDirectory('./application/controllers')
->throwExceptions(true)
->dispatch();

SmartyController.php路由文件:

<?php
class SmartyController extends Zend_Controller_Action
{
function init() {
$this->registry = Zend_Registry::getInstance();
$this->view = $this->registry['smarty'];
$baseurl = $this->_request->getBaseUrl();
$this->view->assign('baseurl',$baseurl);
}
function indexAction(){
$title = "运行成功了。";
$this->view->assign('title',$title);
$this->view->display('smarty/index.phtml');
}
}

最后就是模板文件了:
 
在zendframeworkapplicationviewsscriptssmarty文件夹里面建模板index.phtml:

这里是smarty模板:<{$title}>

这是简单了整合smarty模板。其实写成插件的形式更好,具体方法如下:
index.php入口文件:

<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Shanghai');
define('WEB_ROOT', 'http://localhost/zendframework/');//为分页设置的变量,pagestyle.phtml文件要用
set_include_path('.' .PATH_SEPARATOR .'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR .

get_include_path());

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件

//Smarty的配制在Custom/Controller/Plugin/Smarty.php文件中
Custom_Controller_Plugin_Smarty::SmartyView(); //smarty的配制文件
//配置数据库参数,并连接数据库
$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query("SET NAMES {$config->general->db->config->charset}");
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
Zend_Registry::set('dbprefix',$config->general->db->config->prefix);

//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/zendframework')//设置基本路径
->setParam('noViewRenderer', true)
->setControllerDirectory('./application/controllers')
->throwExceptions(true)
->dispatch();

SmartyController.php路由文件和上面的一样。

在zendframeworkapplicationviewsscriptssmarty文件夹里面建模板index.phtml和上面的一样。
最后就是在zendframeworklibraryCustomControllerPlugin中的插件文件了Smarty.php:

<?php
/**
* Zend_Controller_Plugin_Abstract
*/
//require_once 'Zend/Controller/Plugin/Abstract.php';
class Custom_Controller_Plugin_Smarty extends Zend_Controller_Plugin_Abstract
{
public static function SmartyView(){
//配制smarty参数
include "./public/scripts/Smarty/Smarty.class.php";//引入smarty文件
$smarty = new Smarty();
$smarty -> template_dir = "./application/views/scripts/";//设置模板显示路径
$smarty -> compile_dir = "./application/views/scripts/templates_c";
$smarty -> left_delimiter = "<{";
$smarty -> right_delimiter = "}>";
$registry = Zend_Registry::getInstance();
$registry->set('smarty',$smarty);
}
}

OK,设置成功!

posted @ 2012-04-18 20:46  weiwei~  阅读(936)  评论(0编辑  收藏  举报