亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > PHP > 正文

Zend Framework實現Zend_View集成Smarty模板系統的方法

2024-05-04 23:43:35
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Zend Framework實現Zend_View集成Smarty模板系統的方法,詳細分析了視圖組件Zend_View使用接口Zend_View_Interface繼承Smarty的原理與實現技巧,需要的朋友可以參考下
 

本文實例講述了Zend Framework實現Zend_View集成Smarty模板系統的方法。分享給大家供大家參考,具體如下:

Zend_View抽象出了Zend_View_Interface,可以讓我們集成不同的視圖解決方案,例如可以集成smarty。要在zend中使用其他視圖系統作為視圖,只要實現Zend_View_Interface接口即可。

Zend_View_Interface的接口定義:

<?php/** * Interface class for Zend_View compatible template engine implementations * * @category  Zend * @package  Zend_View * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license  http://framework.zend.com/license/new-bsd   New BSD License */interface Zend_View_Interface{  /**   * Return the template engine object, if any   *   * If using a third-party template engine, such as Smarty, patTemplate,   * phplib, etc, return the template engine object. Useful for calling   * methods on these objects, such as for setting filters, modifiers, etc.   *   * @return mixed   */  public function getEngine();  /**   * Set the path to find the view script used by render()   *   * @param string|array The directory (-ies) to set as the path. Note that   * the concrete view implentation may not necessarily support multiple   * directories.   * @return void   */  public function setScriptPath($path);  /**   * Retrieve all view script paths   *   * @return array   */  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');  /**   * Assign a variable to the view   *   * @param string $key The variable name.   * @param mixed $val The variable value.   * @return void   */  public function __set($key, $val);  /**   * Allows testing with empty() and isset() to work   *   * @param string $key   * @return boolean   */  public function __isset($key);  /**   * Allows unset() on object properties to work   *   * @param string $key   * @return void   */  public function __unset($key);  /**   * Assign variables to the view script via differing strategies.   *   * Suggested implementation is to allow 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);  /**   * 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();  /**   * Processes a view script and returns the output.   *   * @param string $name The script name to process.   * @return string The script output.   */  public function render($name);}

集成Smarty的基本實現如下:

smarty下載地址

http://www.smarty.net/files/Smarty-3.1.7.tar.gz

目錄結構

root@coder-671T-M:/www/zf_demo1# tree
.
├── application
│   ├── Bootstrap.php
│   ├── configs
│   │   └── application.ini
│   ├── controllers
│   │   ├── ErrorController.php
│   │   └── IndexController.php
│   ├── models
│   └── views
│       ├── helpers
│       └── scripts
│           ├── error
│           │   └── error.phtml
│           └── index
│               ├── index.phtml
│               └── index.tpl
├── docs
│   └── README.txt
├── library
│   ├── Lq
│   │   └── View
│   │       └── Smarty.php
│   └── smartylib
│       ├── debug.tpl
│       ├── plugins
│       │   ├── ...........................
│       │   └── variablefilter.htmlspecialchars.php
│       ├── SmartyBC.class.php
│       ├── Smarty.class.php
│       └── sysplugins
│           ├── ..........................
│           └── smarty_security.php
├── public
│   └── index.php
├── temp
│   └── smarty
│       └── templates_c
│           └── 73d91bef3fca4e40520a7751bfdfb3e44b05bdbd.file.index.tpl.php
└── tests
    ├── application
    │   └── controllers
    │       └── IndexControllerTest.php
    ├── bootstrap.php
    ├── library
    └── phpunit.xml

24 directories, 134 files

/zf_demo1/library/Lq/View/Smarty.php

<?phprequire_once 'smartylib/Smarty.class.php';class Lq_View_Smarty implements Zend_View_Interface {  /**   * Smarty object   *   * @var Smarty   */  protected $_smarty;  /**   * Constructor   *   * @param $tmplPath string   * @param $extraParams array   * @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 $path string   *      The directory to set as the path.   * @return void   */  public function setScriptPath($path) {    if (is_readable ( $path )) {      $this->_smarty->template_dir = $path;      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 $path string   * @param $prefix string   *      Unused   * @return void   */  public function setBasePath($path, $prefix = 'Zend_View') {    return $this->setScriptPath ( $path );  }  /**   * Alias for setScriptPath   *   * @param $path string   * @param $prefix string   *      Unused   * @return void   */  public function addBasePath($path, $prefix = 'Zend_View') {    return $this->setScriptPath ( $path );  }  /**   * Assign a variable to the template   *   * @param $key string   *      The variable name.   * @param $val mixed   *      The variable value.   * @return void   */  public function __set($key, $val) {    $this->_smarty->assign ( $key, $val );  }  /**   * Retrieve an assigned variable   *   * @param $key string   *      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 $key string   * @return boolean   */  public function __isset($key) {    return (null !== $this->_smarty->get_template_vars ( $key ));  }  /**   * Allows unset() on object properties to work   *   * @param $key string   * @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 $spec string|array   *      The assignment strategy to use (key or array of key   *      => value pairs)   * @param $value mixed   *      (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 $name string   *      The template to process.   * @return string The output.   */  public function render($name) {    ob_start();    echo $this->_smarty->fetch ( $name );    unset($name);  }}

/zf_demo1/application/configs/application.ini

[production]includePaths.library = APPLICATION_PATH "/../library"bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"appnamespace = "Application"autoloadernamespaces.lq = "Lq_"pluginpaths.Lq_View_Smarty = "Lq/View/Smarty"resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 1phpSettings.display_startup_errors = 1phpSettings.display_errors = 1

/zf_demo1/application/Bootstrap.php

<?phpclass Bootstrap extends Zend_Application_Bootstrap_Bootstrap {  /**   * Initialize Smarty view   */  protected function _initSmarty() {    $smarty = new Lq_View_Smarty ();    $smarty->setScriptPath('/www/zf_demo1/application/views/scripts');    return $smarty;  }}

/zf_demo1/application/controllers/IndexController.php

<?phpclass IndexController extends Zend_Controller_Action {  public function init() {    /*     * Initialize action controller here     */  }  public function indexAction() {    $this->_helper->getHelper('viewRenderer')->setNoRender();    $this->view = $this->getInvokeArg ( 'bootstrap' )->getResource ( 'smarty' );    $this->view->book = 'Hello World! ';    $this->view->author = 'by smarty';    $this->view->render('index/index.tpl');  }}

/zf_demo1/application/views/scripts/index/index.tpl

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>{$book}{$author}</body></html>

如果需要配置smarty可以打開/zf_demo1/library/smartylib/Smarty.class.php文件進行相應配置例如

/*** Initialize new Smarty object**/public function __construct(){    // selfpointer needed by some other class methods    $this->smarty = $this;    if (is_callable('mb_internal_encoding')) {      mb_internal_encoding(Smarty::$_CHARSET);    }    $this->start_time = microtime(true);    // set default dirs    $this->setTemplateDir('/www/zf_demo1/temp/smarty' . DS . 'templates' . DS)      ->setCompileDir('/www/zf_demo1/temp/smarty' . DS . 'templates_c' . DS)      ->setPluginsDir(SMARTY_PLUGINS_DIR)      ->setCacheDir('/www/zf_demo1/temp/smarty' . DS . 'cache' . DS)      ->setConfigDir('/www/zf_demo1/temp/smarty' . DS . 'configs' . DS);    $this->debug_tpl = 'file:' . dirname(__FILE__) . '/debug.tpl';    if (isset($_SERVER['SCRIPT_NAME'])) {      $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);    }}
 


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲女人天堂网| 91免费的视频在线播放| 欧美性色xo影院| 亚洲少妇中文在线| 91精品久久久久久久久不口人| 久久综合五月天| 久久久亚洲影院| 中文字幕视频在线免费欧美日韩综合在线看| 亚洲女人初尝黑人巨大| 欧美激情久久久久久| 精品小视频在线| 精品福利免费观看| 色噜噜国产精品视频一区二区| 日韩精品丝袜在线| 国产精自产拍久久久久久| 美女福利精品视频| 懂色av中文一区二区三区天美| 久久免费高清视频| 亚洲国产一区二区三区在线观看| 欧美电影在线观看| 在线日韩第一页| 久久激情视频免费观看| 国产精品免费一区| 成人免费观看49www在线观看| 亚洲人午夜色婷婷| 97香蕉超级碰碰久久免费的优势| 精品国产欧美一区二区三区成人| 国产拍精品一二三| 在线观看亚洲区| 久久全国免费视频| 亚洲欧美综合图区| 亚洲理论片在线观看| 久久久999成人| 国产欧美精品一区二区三区介绍| 亚洲色图在线观看| 亚洲欧美精品一区| 日韩精品视频在线| 国产欧美一区二区三区四区| 97国产精品视频| 亚洲资源在线看| 久久久亚洲成人| 亚洲性av在线| 黑人巨大精品欧美一区二区| 亚洲欧美日韩精品久久| 国产成人亚洲精品| 2019亚洲日韩新视频| 国产精品女人久久久久久| 亚洲精品中文字幕有码专区| 欧美日韩一区二区三区在线免费观看| 久久久免费av| 久久在线精品视频| 国产精品日日摸夜夜添夜夜av| 欧美激情乱人伦| 欧美激情xxxxx| 韩国v欧美v日本v亚洲| 91免费福利视频| 亚洲一区二区三区xxx视频| 日韩电影中文字幕一区| 亚洲国模精品私拍| 日韩精品在线免费| 国产激情999| 欧美日韩国产麻豆| 91大神在线播放精品| 亚洲a成v人在线观看| 亚洲第一av网站| 欧美日韩国产999| 日韩电视剧免费观看网站| 国产视频精品xxxx| 欧美性猛交xxxx偷拍洗澡| 国内精品久久久久伊人av| 96精品久久久久中文字幕| 在线播放国产精品| 97久久精品视频| 亚洲美女免费精品视频在线观看| 亚洲在线www| 亚洲国产精品中文| 中文字幕av一区二区| 国产精品日韩电影| 国产综合久久久久| 欧美性极品xxxx娇小| 98精品国产自产在线观看| 国产精品va在线播放我和闺蜜| 亚洲最大在线视频| 青草青草久热精品视频在线网站| 欧美日韩国内自拍| 国产精品a久久久久久| 色婷婷综合成人| 精品性高朝久久久久久久| 91精品久久久久久综合乱菊| 国产精品永久免费在线| 欧美视频中文字幕在线| 久久五月情影视| 久热国产精品视频| 日韩电影中文字幕一区| 搡老女人一区二区三区视频tv| 日本在线精品视频| 国产中文字幕日韩| 中文字幕在线国产精品| 欧美激情国内偷拍| 日韩国产精品亚洲а∨天堂免| 伊人精品在线观看| 欧美日本高清视频| 国产在线观看一区二区三区| 中文字幕在线观看亚洲| 亚洲综合国产精品| 精品国产一区二区三区久久久| 国产精品电影网站| 91美女福利视频高清| 亚洲电影av在线| 亚洲色图欧美制服丝袜另类第一页| 精品无人区乱码1区2区3区在线| 中文字幕日韩综合av| 久久精品国产亚洲7777| 69视频在线播放| 国产69精品99久久久久久宅男| 欧美午夜无遮挡| 欧美成人免费在线观看| 成人激情av在线| 精品一区二区三区三区| 狠狠躁天天躁日日躁欧美| 欧美丝袜第一区| 国产精品久久电影观看| 国产国语刺激对白av不卡| 欧美性极品xxxx做受| 日韩的一区二区| 亚洲第一网中文字幕| 欧美性受xxxx白人性爽| 亚洲欧美一区二区精品久久久| 亚洲黄色av网站| 青草热久免费精品视频| 中国人与牲禽动交精品| 国产精品电影网站| 国产精品观看在线亚洲人成网| 91精品国产综合久久久久久久久| 色婷婷久久一区二区| 欧美日韩另类在线| 欧美性猛交xxxx乱大交极品| 色哟哟网站入口亚洲精品| 午夜精品一区二区三区在线视频| 日韩专区在线观看| 欧美日韩中文在线观看| 亚洲欧美日韩中文在线| 成人福利视频网| 久久久在线观看| 国产精品成人一区二区| 久久视频国产精品免费视频在线| 亚洲香蕉伊综合在人在线视看| 国产一区二区在线免费| 久久精品国产电影| 性色av一区二区咪爱| 国产精品久久久久久婷婷天堂| 亚洲一区第一页| 在线观看精品自拍私拍| 国产精品精品一区二区三区午夜版| 亚洲欧美日韩区| 亚洲国产精品久久久久秋霞蜜臀| 亚洲欧美激情在线视频| 伦伦影院午夜日韩欧美限制| 456亚洲影院| 国产精品视频精品视频| 色久欧美在线视频观看| 日韩福利视频在线观看| 成人做爰www免费看视频网站| 亚洲精品之草原avav久久|