mmocom
注册会员
Rank: 2



UID 58
精华 6
积分 70
帖子 21
翻译 0
原创 1
阅读权限 20
注册 2007-6-6
状态 离线
发表于 2008-1-2 17:41  资料  短消息  加为好友  添加 mmocom 为MSN好友 通过MSN和 mmocom 交谈
我的zf与smarty集成



PHP代码如下:

<?php
/**
 *
 * Zend Framework 与 PHP模板引擎SMARTY 的配置
 * 
 *
 */
require_once 'Custom/View/Smarty/Smarty.class.php';

class 
Custom_View_Smarty implements Zend_View_Interface
{
    
/**
     * Smarty object     
     * @var Smarty     
     */   
    
protected $_smarty;
    
    
/**
     * Constructor
     *
     * @param string $tmplPath
     * @param array $extraParams
      * @return void
      */    
    
public function __construct(array $smartyParams = array())
    {
        
//检测SMARTY配置参数是否合格
        
$this->_checkRequiredOptions($smartyParams);
        
        
$this->_smarty = new Smarty;

        foreach ((array) 
$smartyParams as $key => $value)
        {
            
$this->_smarty->$key $value;
        }
    }
    
/**     
     * Return the template engine object     
     * 返回模板引擎对象    
     * @return Smarty     
     */    
    
public function getEngine()    
    {       
         return 
$this->_smarty;    
    }
    
    
    public function 
setScriptPath($path)    
    {       
    }
    

    public function 
getScriptPaths()
    {
    }

    
/**
     * Set a base path to all view resources
     * 
     * @param  string $path 
     * @param  string $classPrefix 
     * @return void
     */
    
public function setBasePath($path$classPrefix 'Zend_View')
    {
    }

    
/**
     * Add an additional path to view resources
     * 
     * @param  string $path 
     * @param  string $classPrefix 
     * @return void
     */
    
public function addBasePath($path$classPrefix 'Zend_View')
    {
    }
    
/**     
     * 设置模板路径     
      *     
     * @param string $path The directory to set as the path.     
     * @return void     
     */ 
    
public function setTemplatePath()
    {
        if (
is_readable$this->_smarty->template_dir ))
        {            
            return 
$this->_smarty->template_dir;
        }
        throw new 
Exception('Invalid TemplatePath provided');
    }  
    
/**     
     * 设置模板编译路径     
      *     
     * @param string $path The directory to set as the path.     
     * @return void     
     */ 
    
public function setCompilePath()
    {
        if (
is_readable$this->_smarty->compile_dir))
        {            
            return 
$this->_smarty->compile_dir;
        }
        throw new 
Exception('Invalid CompilePath provided');
    }
    
    public function 
getTemplatePath()
    {
        return 
$this->_smarty->template_dir;
    }   
    
    
/**     
     * 分配变量给模板 Assign a variable to the template     
     *     
     * @param string $key The variable name.     
     * @param mixed $val The variable value.     
     * @return void     
     */    
    
public function __set($key$val)    
    {        
        
$this->_smarty->assign($key$val);    
    }    
    
/**     
     * Retrieve an assigned variable     
     *     
     * @param string $key The variable name.     
     * @return mixed The variable value.     
     */    
    
public function __get($key)    
    {        
        return 
$this->_smarty->get_template_vars($key);    
    }    
    
/**     
     * Allows testing with empty() and isset() to work     
     *     
      * @param string $key     
     * @return boolean     
     */            
    
public function __isset($key)    
    {        
        return (
null !== $this->_smarty->get_template_vars($key));    
    }    
    
/**     
     * Allows unset() on object properties to work     
     *     
     * @param string $key    
     * @return void     
     */   
    
public function __unset($key)    
    {        
        
$this->_smarty->clear_assign($key);    
    }   
    
/**     
     * 分配变量给模板 Assign variables to the template    
     *     
     * Allows setting a specific key to the specified value, OR passing an array     
     * of key => value pairs to set en masse.     
     *     
     * @see __set()     
     * @param string|array $spec The assignment strategy to use (key or array of key    
     * => value pairs)     
     * @param mixed $value (Optional) If assigning a named variable, use this     
     * as the value.     
     * @return void     
     */    
    
public function assign($spec$value null)    
    {        
        if (
is_array($spec))
        {            
            
$this->_smarty->assign($spec);            
            return;        
        }        
        
$this->_smarty->assign($spec$value);    
    }    
    
/**     
     * 清楚所有已经分配变量 Clear all assigned variables     
     *     
     * Clears all variables assigned to Zend_View either via {@link assign()} or     
     * property overloading ({@link __get()}/{@link __set()}).     
     *     
     * @return void     
     */    
    
public function clearVars()    
    {        
        
$this->_smarty->clear_all_assign();    
    }    
    
/**     
     * 处理模板并返回输出的内容 Processes a template and returns the output.     
     *     
     * @param string $name The template to process.     
     * @return string The output.     
     */    
    
public function render($name)    
    {        
        return 
$this->_smarty->fetch($name);    
    }
    
    
/**     
     * 处理模板并显示输出内容 Processes a template and display the output.     
     *     
     * @param string $name The template to process.     
     * @return string The output.     
     */     
    
public function display($name)    
    {        
        return 
$this->_smarty->display($name);    
    }
    
    
     
/**
     * Check for extraParams options that are mandatory.
     * 检测 extraParams 的参数项
     * Throw exceptions if any are missing.
     * 抛出错误异常
     * @param array $extraParams
     * @throws Zend_View_Exception
     */
    
protected function _checkRequiredOptions(array $extraParams)
    {
        
// we need at least a template_dir and compile_dir
        
if (! array_key_exists('template_dir'$extraParams)) {
            require_once 
'Zend/View/Exception.php';
            throw new 
Zend_View_Exception("Configuration array must have a key for 'template_dir' for Smarty View.",$this);
        }

        if (! 
array_key_exists('compile_dir'$extraParams)) {
            
/**
             * @see Zend_View_Exception
             */
            
require_once 'Zend/View/Exception.php';
            throw new 
Zend_View_Exception("Configuration array must have a key for 'compile_dir' for Smarty Compile.",$this);
        }

    }
    
}

?>


把下载的smarty文件和这个文件 一同放入你项目路径里的library\Custom\View下
smarty的目录library\Custom\View\Smarty

调用方法

PHP代码如下:

<?php
class IndexController extends Zend_Controller_Action
{
    public function 
init()
    {
        
$this->tpl Zend_Registry::get('systpl');
    }

    public function 
indexAction()
    {
                
$this->tpl->test "123";
        
$this->tpl->display('index.html');
    }
}
?>


index.html


[ 本帖最后由 mmocom 于 2008-1-2 17:44 编辑 ]

顶部
haohappy
超级版主
Rank: 8Rank: 8
PHPEye站长


UID 2
精华 11
积分 110
帖子 260
翻译 6
原创 1
阅读权限 150
注册 2007-5-2
状态 离线
发表于 2008-1-2 22:35  资料  短消息  加为好友  添加 haohappy 为MSN好友 通过MSN和 haohappy 交谈
经典问题的解决方案,加入精华。  虽然我自己觉得Zend_View很好用





互助共享,共同成长。
顶部
bc1998
新手上路
Rank: 1



UID 588
精华 0
积分 0
帖子 30
翻译 0
原创 0
阅读权限 10
注册 2007-11-24
状态 离线
发表于 2008-1-2 23:07  资料  短消息  加为好友 
这样做zend_view_help还能用了 吗

顶部
smallipis
新手上路
Rank: 1



UID 636
精华 0
积分 0
帖子 6
翻译 0
原创 0
阅读权限 10
注册 2007-12-12
状态 离线
发表于 2008-1-3 00:55  资料  短消息  加为好友 
$this->tpl = Zend_Registry::get('systpl');
怎么来的....

index.php文件又是怎么配置...能详细点吗?

对于我新手来说,没指示配置是个难题哦....

顶部
mmocom
注册会员
Rank: 2



UID 58
精华 6
积分 70
帖子 21
翻译 0
原创 1
阅读权限 20
注册 2007-6-6
状态 离线
发表于 2008-1-3 11:30  资料  短消息  加为好友  添加 mmocom 为MSN好友 通过MSN和 mmocom 交谈
回复 #4 smallipis 的帖子

你可以在 http://www.phpeye.com/bbs/thread-313-1-1.html 找到相关信息

顶部
bc1998
新手上路
Rank: 1



UID 588
精华 0
积分 0
帖子 30
翻译 0
原创 0
阅读权限 10
注册 2007-11-24
状态 离线
发表于 2008-1-3 12:46  资料  短消息  加为好友 


QUOTE:
原帖由 bc1998 于 2008-1-2 23:07 发表
这样做zend_view_help还能用了 吗


顶部
玉面修罗
新手上路
Rank: 1



UID 290
精华 0
积分 0
帖子 14
翻译 0
原创 0
阅读权限 10
注册 2007-8-17
状态 离线
发表于 2008-1-3 14:33  资料  短消息  加为好友 
这种适配器方式应该不会对原来程序造成影响..其他功能应该都可以正常使用..
接口是一致的..

顶部
binderyi
新手上路
Rank: 1



UID 320
精华 0
积分 0
帖子 10
翻译 0
原创 0
阅读权限 10
注册 2007-8-29
状态 离线
发表于 2008-1-14 14:58  资料  短消息  加为好友 
自己感觉smarty很难学

顶部
bc1998
新手上路
Rank: 1



UID 588
精华 0
积分 0
帖子 30
翻译 0
原创 0
阅读权限 10
注册 2007-11-24
状态 离线
发表于 2008-1-14 18:48  资料  短消息  加为好友 


QUOTE:
原帖由 bc1998 于 2008-1-2 23:07 发表
这样做zend_view_help还能用了 吗


顶部
Cykit
新手上路
Rank: 1



UID 729
精华 0
积分 0
帖子 15
翻译 0
原创 0
阅读权限 10
注册 2008-1-14
状态 离线
发表于 2008-1-14 21:53  资料  短消息  加为好友 
Smarty应该是“真正的PHPMVC架构”(ZF,CAKEPHP等)还没出来的一种实现展现与代码相分离的产物。在ZF等MVC架构已经出来了的今天,为什么还要使用Smarty?LZ能否说明一下使用Smaty的好处?

习惯了?有缓存?还是?

两年前第一次用PHP做网站的时候,用了Smaty,真的很不错。不过这两年来没用过PHP做过项目,只是在闲暇时间学习学习而已,孤陋寡闻,还请指教!!

本人不大会说话,如果有任何一句话让LZ不舒服,在这里先说声抱歉!!!

顶部
 


PHPEye社区--LAMP开源技术社区


当前时区 GMT+8, 现在时间是 2008-7-21 02:04

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.017650 second(s), 7 queries , Gzip enabled

清除 Cookies - 联系我们 - PHPEye开源社区 - Archiver - WAP