---
title: "用Smarty作为Zend Framework的视图"
author: "ranfow"
date: "2008-08-21T23:10:49Z"
tags: []
slug: "smarty-zend-framework"
draft: false
lastmod: "2008-08-21T23:10:49Z"
id: 231370
url: "https://ranfow.iteye.com/blog/231370"
---

一个优秀模板与一个优秀框架的结合。  
首先用Smarty实现View的接口。  
Smarty.php：

&lt;?php  
/\**  
 * Smarty.php  
 * Smarty视图类  
 * created by ranfow(ranfow@qq.com)  
 * 2008-08-19  
 \*/  
require\_once 'Zend/View/Interface.php';  
require\_once 'Smarty/Smarty.class.php';

class Zend\_View\_Smarty implements Zend\_View\_Interface  
{  
    /\**  
     * Smarty object  
     * @var Smarty  
     \*/  
    protected $\_smarty;

    /\**  
     * Constructor  
     *  
     * @param string $tmplPath  
     * @param array $extraParams  
     * @return void  
     \*/  
    public function \_\_construct($tmplPath = null, $extraParams = array())  
    {  
        $this-&gt;\_smarty = new Smarty;

        if (null !== $tmplPath) {  
            $this-&gt;setScriptPath($tmplPath);  
        }

        foreach ($extraParams as $key =&gt; $value) {  
            $this-&gt;\_smarty-&gt;$key = $value;  
        }  
    }

    /\**  
     * Return the template engine object      
     *  
     * @return Smarty  
     \*/  
    public function getEngine()  
    {  
        return $this-&gt;\_smarty;  
    }

    /\**  
     * Set the path to the templates  
     *  
     * @param string $path The directory to set as the path.  
     * @return void  
     \*/  
    public function setScriptPath($path)  
    {  
        if (is\_readable($path)) {  
            $this-&gt;\_smarty-&gt;template\_dir = $path;  
            return;  
        }

        throw new Exception('Invalid path provided');  
    }

    /\**  
     * Retrieve the current template directory  
     *  
     * @return string  
     \*/  
    public function getScriptPaths()  
    {  
        return array($this-&gt;\_smarty-&gt;template\_dir);  
    }

    /\**  
     * Alias for setScriptPath  
     *  
     * @param string $path  
     * @param string $prefix Unused  
     * @return void  
     \*/  
    public function setBasePath($path, $prefix = 'Zend\_View')  
    {  
        return $this-&gt;setScriptPath($path);  
    }

    /\**  
     * Alias for setScriptPath  
     *  
     * @param string $path  
     * @param string $prefix Unused  
     * @return void  
     \*/  
    public function addBasePath($path, $prefix = 'Zend\_View')  
    {  
        return $this-&gt;setScriptPath($path);  
    }

    /\**  
     * 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-&gt;\_smarty-&gt;assign($key, $val);  
    }

    /\**  
     * Retrieve an assigned variable  
     *  
     * @param string $key The variable name.  
     * @return mixed The variable value.  
     \*/  
    public function \_\_get($key)  
    {  
        return $this-&gt;\_smarty-&gt;get\_template\_vars($key);  
    }

    /\**  
     * Allows testing with empty() and isset() to work  
     *  
     * @param string $key  
     * @return boolean  
     \*/  
    public function \_\_isset($key)  
    {  
         return (null !== $this-&gt;\_smarty-&gt;get\_template\_vars($key));  
    }

    /\**  
     * Allows unset() on object properties to work  
     *  
     * @param string $key  
     * @return void  
     \*/  
    public function \_\_unset($key)  
    {  
        $this-&gt;\_smarty-&gt;clear\_assign($key);  
    }

    /\**  
     * Assign variables to the template  
     *  
     * Allows setting a specific key to the specified value, OR passing an array  
     * of key =&gt; value pairs to set en masse.  
     *  
     * @see \_\_set()  
     * @param string|array $spec The assignment strategy to use (key or array of key  
     * =&gt; 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-&gt;\_smarty-&gt;assign($spec);  
            return;  
        }

        $this-&gt;\_smarty-&gt;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-&gt;\_smarty-&gt;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-&gt;\_smarty-&gt;fetch($name);  
    }  
      
    /\**  
     * Processes a template and display it to the browser.  
     *  
     * @param string $name The template to process.  
     \*/  
    public function display($name)  
    {  
            return $this-&gt;\_smarty-&gt;display($name);  
    }  
}

初始化：

$smarty = new Zend\_View\_Smarty("./templates", array (  
    "compile\_dir"     =&gt; "./templates\_c",  
    "cache\_dir"       =&gt; "./cache",  
    "config\_dir"      =&gt; "./config",  
    "left\_delimiter"  =&gt; "{{",  
    "right\_delimiter" =&gt; "}}"  
));

模板index.tpl：

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
    &lt;head&gt;  
        &lt;title&gt;{{$title}}&lt;/title&gt;  
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
    {{$name}}  
    &lt;/body&gt;  
&lt;/html&gt;

赋值、解释并输出：

......  
$smarty-&gt;title = 'test';  
$smarty-&gt;name = 'ranfow';  
 // $smarty-&gt;display('index.tpl');       // 这种为直接输出  
 $rendered = $smarty-&gt;render('index.tpl');  
 echo $rendered;  
......