Zend Framework整合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参数
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');
     }

}
最后就是模板文件了:
在zendframework/application/views/scripts/smarty文件夹里面建模板index.phtml:

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

这是简单了整合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的配制在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路由文件和上面的一样。
在zendframework/application/views/scripts/smarty文件夹里面建模板index.phtml:和上面的一样。

最后就是在zendframework/library/Custom/Controller/Plugin中的插件文件了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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值