文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>集成Smarty

集成Smarty

时间:2010-03-31  来源:bing_fox

1. 简介
用Smarty来代替ZF的view.即用变量来组织Html结构.
2. 整合
有两种方式
2.1 直接
.1 关闭ZF的view功能
在application.ini文件中
===
resources.frontController.noViewRenderer = 1
===
.2 配置smarty
在application.ini文件中,smarty目录及文件事先准备好
===
[smarty]
smarty.class_path = APPLICATION_PATH "/../smarty/smarty/Smarty.class.php"
smarty.left_delimiter = "<*"
smarty.right_delimiter = "*>"
smarty.template_dir = APPLICATION_PATH "/../smarty/templates"
smarty.compile_dir = APPLICATION_PATH "/../smarty/templates_c"
smarty.cache_dir = APPLICATION_PATH "/../smarty/cache"
smarty.cache_lifetime = 600
smarty.caching = 1
===
.3 初始化smarty
在Bootstrap.php中
===
 public function _initView(){
    //从配置文件中读取smarty的配置,CONFIG_FILE_PATH在index.php中定义
  $config = new Zend_Config_Ini(CONFIG_FILE_PATH.'/application.ini', 'smarty'); 
  //加载smarty库
  require_once $config->smarty->class_path;
  //初始化smarty库
  $smarty = new Smarty();
  $smarty->left_delimiter = $config->smarty->left_delimiter;
  $smarty->right_delimiter = $config->smarty->right_delimiter;
  $smarty->template_dir = $config->smarty->template_dir;
  $smarty->compile_dir = $config->smarty->compile_dir;
  $smarty->cache_dir = $config->smarty->cache_dir;
  $smarty->cache_lifetime = $config->smarty->cache_lifetime;
  $smarty->caching = $config->smarty->caching;
  //将$smarty存入注册器
  Zend_Registry::set('smarty', $smarty);
 }
===
.4 使用
在IndexController.php文件中
===
    //声明一个私有变量
    private $smarty;
    public function init()
    {
     //$this->_helper->viewRenderer->setNoRender(); //禁止view
     //从注册器提取$smarty
     $this->smarty = Zend_Registry::get('smarty');
        /* Initialize action controller here */
    }
    public function indexAction()
    {
      //使用
     $hello = "Hello World!";
     $name = "Jack";
     $this->smarty->assign("hello",$hello);
     $this->smarty->assign("name",$name);
     $this->smarty->display('index.tpl');
    }
===
    小插曲:{$var}与html字符串连接, "Hello {$name}"
2.2 实现view的抽象
.1 在ZendViewSmarty.php文件中
===
<?php
require_once '../library/Smarty/Smarty.class.php';
require_once 'Zend/View/Interface.php';
class Zend_View_Smarty implements Zend_View_Interface
{
    /**
     * Smarty object
     * @var Smarty
     */
    private $_smarty;
 
    /**
     * Constructor
     *
     * @param string $tmplPath
     * @param array $extraParams
     * @return void
     */
    public function __construct($tmplPath = null, $extraParams = array())
    {
        $this->_smarty = new Smarty;
 
        if (null !== $tmplPath) {
            $this->setScriptPath($tmplPath);
        }
 
        foreach ($extraParams as $key => $value) {
            $this->_smarty->$key = $value;
        }
    }
 
    /**
     * Return the template engine object
     *
     * @return Smarty
     */
    public function getEngine()
    {
        return $this->_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->_smarty->template_dir = $path;
            $this->_smarty->compile_dir = $path."/../templates_c";
            return;
        }
 
        throw new Exception('Invalid path provided');
    }
 
    /**
     * Retrieve the current template directory
     *
     * @return string
     */
    public function getScriptPaths()
    {
        return array($this->_smarty->template_dir);
    }
 
    /**
     * Alias for setScriptPath
     *
     * @param string $path
     * @param string $prefix Unused
     * @return void
     */
    public function setBasePath($path, $prefix = 'Zend_View')
    {
        return $this->setScriptPath($path);
    }
 
    /**
     * Alias for setScriptPath
     *
     * @param string $path
     * @param string $prefix Unused
     * @return void
     */
    public function addBasePath($path, $prefix = 'Zend_View')
    {
        return $this->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->_smarty->assign($key, $val);
    }
   
    //为了获取_smarty变量
    public function __get($property_name){
   if(isset($this->$property_name)){
   return($this->$property_name);
  }else{
   return(NULL);
  }
  }
    /**
     * 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);
    }
}
?>
===
.2 在index.php中引入该类
require_once 'ZendViewSmarty.php';
.3 在controller中初始化
    public function init()
    {
     $this->view   = new Zend_View_Smarty(APPLICATION_PATH.'/../templates');
        $viewRenderer = $this->_helper->getHelper('viewRenderer');
        $viewRenderer->setView($this->view)
                     ->setViewBasePathSpec($this->view->_smarty->template_dir)
                     ->setViewScriptPathSpec(':controller/:action.:suffix')
                     ->setViewScriptPathNoControllerSpec(':action.:suffix')
                     ->setViewSuffix('tpl');
     
        /* Initialize action controller here */
    }
.4 使用
在controller中使用与$this->view一样;
在*.tpl中却要以Smarty的语法读取变量;
.5 一些说明
输出到templates/controller/action.tpl文件  //如templates/index/index.tpl
在tpl中include时,当前目录为templates
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载