zend 融合 smarty 用法概要

Zend Framework本身只带的View虽然很强大,但还是不能很有效的实现代码与美工的分离,而Smarty的特点就能填补这个缺陷,当然萝卜青菜各有所爱,这里也只是介绍一个例子,抛砖引玉。

      要在Zend框架中调用Smarty,要把Smarty当成Zend的一个插件来用,作为Zend这个完全面向对象的框架来说,插件肯定也要是完全面向对象,好在Smarty就是一个类,所以只要很简单的修改,就能用于Zend中。

     以投票系统为例,目录结构如下

 

    模板文件放在application/views/templates中,同时要建立application/views/templates_c和application/views/templates_c/cache_c文件夹。我把Smarty文件夹放在library文件夹下,这里采用include方法加载Smarty,当然也可以以插件形式。

        在index.php中加载Smarty。

view plaincopy to clipboardprint?
<?php  
    /*
     * Date: 2009.8.16
     * Author:DaGui
     * Email:daguizhang@gmail.com
     * QQ:308713166
     */
    error_reporting(E_ALL|E_STRICT);  
    date_default_timezone_set('Asia/Shanghai');  
    define('WEB_ROOT', 'http://192.168.12.190:81/vote/');  
    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 自动载入类文件  
    $registry = Zend_Registry::getInstance();   
      
    //配置数据库参数,并连接数据库  
    $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 UTF8');  
    Zend_Db_Table::setDefaultAdapter($dbAdapter);  
    Zend_Registry::set('dbAdapter',$dbAdapter);  
      
    //对smarty模版进行初始化  
    include 'Smarty/Smarty.class.php';  
    $views = new Smarty();  
    $views->left_delimiter = "{{";  
    $views->right_delimiter = "}}";  
    $views->compile_dir = 'application/views/templates_c';  
    $views->cache_dir = 'application/views/templates_c/cache_c';  
    $views->template_dir = 'application/views/templates';  
    function smarty_block_dynamic($param,$content,&$views)  
    {  
        return $content;  
    }  
    $views->register_block('dynamic','smarty_block_dynamic',false);  
    Zend_Registry::set('views', $views);  
   
    //创建验证对象  
    $auth = Zend_Auth::getInstance();  
    //创建访问权限对象  
    $acl = new Common_Plugin_MyAcl();  
      
    //设置控制器  
    $frontController =Zend_Controller_Front::getInstance();  
    $frontController->setBaseUrl('/vote')//设置基本路径  
                    ->setParam('noViewRenderer', true)  
                    ->registerPlugin(new Common_Plugin_MyAuth($auth, $acl))  
                    ->registerPlugin(new Zend_Controller_Plugin_ErrorHandler())  
                    ->setControllerDirectory('./application/controllers')  
                    ->throwExceptions(true)  
                    ->dispatch();  
?>
<?php
    /*
     * Date: 2009.8.16
     * Author:DaGui
     * Email:daguizhang@gmail.com
     * QQ:308713166
     */
    error_reporting(E_ALL|E_STRICT);
    date_default_timezone_set('Asia/Shanghai');
    define('WEB_ROOT', 'http://192.168.12.190:81/vote/');
    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 自动载入类文件
    $registry = Zend_Registry::getInstance();
   
    //配置数据库参数,并连接数据库
    $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 UTF8');
    Zend_Db_Table::setDefaultAdapter($dbAdapter);
    Zend_Registry::set('dbAdapter',$dbAdapter);
   
    //对smarty模版进行初始化
    include 'Smarty/Smarty.class.php';
    $views = new Smarty();
    $views->left_delimiter = "{{";
    $views->right_delimiter = "}}";
    $views->compile_dir = 'application/views/templates_c';
    $views->cache_dir = 'application/views/templates_c/cache_c';
    $views->template_dir = 'application/views/templates';
    function smarty_block_dynamic($param,$content,&$views)
    {
        return $content;
    }
    $views->register_block('dynamic','smarty_block_dynamic',false);
    Zend_Registry::set('views', $views);

    //创建验证对象
    $auth = Zend_Auth::getInstance();
    //创建访问权限对象
    $acl = new Common_Plugin_MyAcl();
   
    //设置控制器
    $frontController =Zend_Controller_Front::getInstance();
    $frontController->setBaseUrl('/vote')//设置基本路径
                    ->setParam('noViewRenderer', true)
                    ->registerPlugin(new Common_Plugin_MyAuth($auth, $acl))
                    ->registerPlugin(new Zend_Controller_Plugin_ErrorHandler())
                    ->setControllerDirectory('./application/controllers')
                    ->throwExceptions(true)
                    ->dispatch();
?>

 

 

然后在Controller中

 

view plaincopy to clipboardprint?
<?php  
class IndexController extends Zend_Controller_Action  
{  
    /**
     * Date: 2009.8.16
     * Author:DaGui
     * Email:daguizhang@gmail.com
     * QQ:308713166
     */
    protected $views; /*模板对象*/
    protected $data; /*传递模版变量的对象*/
    function init()  
    {  
        $this->registry = Zend_Registry::getInstance();  
        $this->views = Zend_Registry::get('views');  
        $this->data = $this->_request->getBaseUrl();  
    }  
   
    /**
     * 输出首页
     */
    public function indexAction()  
    {  
        $topic=new Topic();//实例化数据库类  
        $page =1;//高置默认页  
        $numPerPage = 6;//每页显示的条数  
        $arrayTopic=$topic->getAllActiveTopic();  
        $total=count($arrayTopic);  
        //显示模版  
        $this->views->assign('baseUrl',$this->data);  
        $this->views->assign('topic',$arrayTopic);  
        $this->views->assign('total',$total);  
        $this->views->display('main.html');  
    }  
}
<?php
class IndexController extends Zend_Controller_Action
{
    /**
     * Date: 2009.8.16
     * Author:DaGui
     * Email:daguizhang@gmail.com
     * QQ:308713166
     */
    protected $views; /*模板对象*/
    protected $data; /*传递模版变量的对象*/
    function init()
    {
        $this->registry = Zend_Registry::getInstance();
        $this->views = Zend_Registry::get('views');
        $this->data = $this->_request->getBaseUrl();
    }

    /**
     * 输出首页
     */
    public function indexAction()
    {
        $topic=new Topic();//实例化数据库类
        $page =1;//高置默认页
        $numPerPage = 6;//每页显示的条数
        $arrayTopic=$topic->getAllActiveTopic();
        $total=count($arrayTopic);
        //显示模版
        $this->views->assign('baseUrl',$this->data);
        $this->views->assign('topic',$arrayTopic);
        $this->views->assign('total',$total);
        $this->views->display('main.html');
    }
}

 


在相应的view中

 

view plaincopy to clipboardprint?
<body>
   
<div class="RoundedCorner">
<div class="nav"><b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b></b><b class="rtop"><b class="r4"></b></b> 投票系统</div>
<div class="content">
<h4>最近投票主题</h4>
<ul>
{{section name=lo loop=$topic}}  
<li><a href="{{$baseUrl}}/index/showtopic/tid/{{$topic[lo].id}}" mce_href="{{$baseUrl}}/index/showtopic/tid/{{$topic[lo].id}}" >{{$topic[lo].title}}</a></li>
{{/section}}  
</ul>
</div>
    <b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b></p>
    </div>
</body>
</html>
<body>

<div class="RoundedCorner">
<div class="nav"><b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b></b><b class="rtop"><b class="r4"></b></b> 投票系统</div>
<div class="content">
<h4>最近投票主题</h4>
<ul>
{{section name=lo loop=$topic}}
<li><a href="{{$baseUrl}}/index/showtopic/tid/{{$topic[lo].id}}" mce_href="{{$baseUrl}}/index/showtopic/tid/{{$topic[lo].id}}" >{{$topic[lo].title}}</a></li>
{{/section}}
</ul>
</div>
    <b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b></p>
    </div>
</body>
</html>

posted on 2010-04-30 22:05  webblink  阅读(841)  评论(0编辑  收藏  举报