搜索
首页后端开发php教程Zend Framework框架Smarty扩展实现方法_php实例

本文实例讲述了Zend Framework框架Smarty扩展实现方法。分享给大家供大家参考,具体如下:

今天总结一下ZF框架中扩展Smarty模板的方法,在ZF帮助文档中已经有比较详细的介绍,在这我稍微多说一些。

一.将smarty的核心文件包放在lib文件夹下,文件包中要包括(internals/,plugins/,Config_File.class.php,Smarty.class.php,Smarty_Compiler.class.php,debug.tpl).

二.在Zend/View下添加文件:Smarty.php ,文件的内容如下:

<&#63;php
/**
* Zend_View_Interface
*/
require_once 'Zend/View/Interface.php';
/**
* Smarty 
*/
require_once("smarty/Smarty.class.php");
/**
* 创建Smarty视图
*/
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->_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;
      return;
    }
    throw new Exception('Invalid path provided');
  }
  /**
  * set smarty缓存
  * @author lengfeng
  */
  public function setCompilePath($path){
    if (is_readable($path)) {
      $this->_smarty->compile_dir = $path;
      return;
    }
    throw new Exception('Invalid path provided');    
  }
  /**
  * set smarty 编译后文档
  * @author lengfeng
  */
  public function setCachePath($path){
    if (is_readable($path)) {
      $this->_smarty->cache_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 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);
  }
  /**
   * 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);
  }
  /**
   * 设置是否生成缓存
   * 如果没有参数,默认为true
   */
  public function setCache($bool){
     if (isset($bool)) {
      $this->_smarty->caching = $bool;
      return;
    }
  }
}

三.在app文件夹下创建cache ,compile 文件夹

四.在config.ini 配置文件中加入

dir.compile    = ../app/compile
dir.cache    = ../app/cache

三,四两步可以参见前面关于zendfreamwork框架搭建网站相关教程

五.在application.php 文件中添加

/**
* 初始化smarty视图
*
*/
private function _initSmartyView()
{
    $view = new Zend_View_Smarty();
    $view->setBasePath($this->_pathConfig->dir->viewBase);
    $view->setScriptPath($this->_pathConfig->dir->viewBase."/scripts");
    $view->setCompilePath($this->_pathConfig->dir->compile);
    $view->setCachePath($this->_pathConfig->dir->cache);
    $smarty=$view->getEngine();
    $smarty->caching=false;
    $smarty->debugging = true;
    $smarty->compile_check = true;    
    $smarty->left_delimiter = "<{"; //定义标示符
    $smarty->right_delimiter = "}>";
    $registry = Zend_Registry::getInstance();
    $registry->set('smartyview',$smarty); //smarty对象
    $registry->set('sview',$view);          
}

并在 函数 init()中加入

$this->_initSmartyView();

六.在Controller中调用

因为已经将对象注册,所以可以如下调用:

$view = Zend_Registry::getInstance()->get("smartyview");
//注意这是smarty对象,使用smarty的那些语法,比如 $view->assign("user","root");
$view = Zend_Registry::getInstance()->get("sview"); 
//这是zf的view对象,按zf中的那些方法用,不用改变。
//按这样,你如果要将以前写的代码改为用smaty,后台不用变了,只需要将视图文件改变就行了

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Microsoft NET Framework 安装问题 错误代码 0x800c0006 修复Microsoft NET Framework 安装问题 错误代码 0x800c0006 修复May 05, 2023 pm 04:01 PM

.NETFramework4是开发人员和最终用户在Windows上运行最新版本的应用程序所必需的。但是,在下载安装.NETFramework4时,许多用户抱怨安装程序在中途停止,显示以下错误消息-“ .NETFramework4hasnotbeeninstalledbecauseDownloadfailedwitherrorcode0x800c0006 ”。在您的设备上安装.NETFramework4时,如果您也在体验它,那么您就来对了地方

如何在 Windows 11/10 上使用 SetupDiag 识别 Windows 升级问题如何在 Windows 11/10 上使用 SetupDiag 识别 Windows 升级问题Apr 17, 2023 am 10:07 AM

每当您的Windows11或Windows10PC出现升级或更新问题时,您通常会看到一个错误代码,指示故障背后的实际原因。但是,有时,升级或更新失败可能不会显示错误代码,这时就会出现混淆。有了方便的错误代码,您就可以确切地知道问题出在哪里,因此您可以尝试修复。但是由于没有出现错误代码,因此识别问题并解决它变得极具挑战性。这会占用您大量时间来简单地找出错误背后的原因。在这种情况下,您可以尝试使用Microsoft提供的名为SetupDiag的专用工具,该工具可帮助您轻松识别错误背后的真

SCNotification 已停止工作 [修复它的 5 个步骤]SCNotification 已停止工作 [修复它的 5 个步骤]May 17, 2023 pm 09:35 PM

作为Windows用户,您很可能会在每次启动计算机时遇到SCNotification已停止工作错误。SCNotification.exe是一个微软系统通知文件,由于权限错误和点网故障等原因,每次启动PC时都会崩溃。此错误也以其问题事件名称而闻名。因此,您可能不会将其视为SCNotification已停止工作,而是将其视为错误clr20r3。在本文中,我们将探讨您需要采取的所有步骤来修复SCNotification已停止工作,以免它再次困扰您。什么是SCNotification.e

Microsoft .NET Framework 4.5.2、4.6 和 4.6.1 将于 2022 年 4 月终止支持Microsoft .NET Framework 4.5.2、4.6 和 4.6.1 将于 2022 年 4 月终止支持Apr 17, 2023 pm 02:25 PM

已安装Microsoft.NET版本4.5.2、4.6或4.6.1的MicrosoftWindows用户如果希望Microsoft将来通过产品更新支持该框架,则必须安装较新版本的Microsoft框架。据微软称,这三个框架都将在2022年4月26日停止支持。支持日期结束后,产品将不会收到“安全修复或技术支持”。大多数家庭设备通过Windows更新保持最新。这些设备已经安装了较新版本的框架,例如.NETFramework4.8。未自动更新的设备可能

适用于 Windows 11 的KB5012643破坏了.NET Framework 3.5应用程序适用于 Windows 11 的KB5012643破坏了.NET Framework 3.5应用程序May 09, 2023 pm 01:07 PM

自我们谈论影响安装KB5012643forWindows11的用户的新安全模式错误以来已经过去了一周。这个讨厌的问题并没有出现在微软在发布当天发布的已知问题列表中,因此让所有人都感到意外。好吧,就在您认为情况不会变得更糟的时候,微软为安装此累积更新的用户投下了另一颗炸弹。Windows11Build22000.652导致更多问题因此,这家科技公司警告Windows11用户,他们在启动和使用某些.NETFramework3.5应用程序时可能会遇到问题。听起来很熟悉?不过请不要惊

如何在Zend框架中使用ACL(Access Control List)进行权限控制如何在Zend框架中使用ACL(Access Control List)进行权限控制Jul 29, 2023 am 09:24 AM

如何在Zend框架中使用ACL(AccessControlList)进行权限控制导言:在一个Web应用程序中,权限控制是至关重要的一项功能。它可以确保用户只能访问其有权访问的页面和功能,并防止未经授权的访问。Zend框架提供了一种方便的方法来实现权限控制,即使用ACL(AccessControlList)组件。本文将介绍如何在Zend框架中使用ACL

PHP实现框架:Zend Framework入门教程PHP实现框架:Zend Framework入门教程Jun 19, 2023 am 08:09 AM

PHP实现框架:ZendFramework入门教程ZendFramework是PHP开发的一种开源网站框架,目前由ZendTechnologies维护,ZendFramework采用了MVC设计模式,提供了一系列可重用的代码库,服务于实现Web2.0应用程序和Web服务。ZendFramework深受PHP开发者的欢迎和推崇,拥有广泛

酷冷至尊携手Framework推出创新迷你机箱套件,兼容笔记本主板酷冷至尊携手Framework推出创新迷你机箱套件,兼容笔记本主板Dec 15, 2023 pm 05:35 PM

12月9日消息,最近,酷冷至尊在台北电脑展上的一次展示活动中,展示了与笔记本模块化方案提供商framework合作的迷你机箱套件,这个套件的独特之处在于,它可以兼容并安装来自framework笔记本的主板。目前,这款产品已经开始在市场上销售,售价定为39美元,按当前汇率折合大约279元人民币。这款机箱套件的型号被命名为“frameWORKMAINBOARDCASE”。在设计方面,它体现了极致的紧凑与实用性,尺寸仅为297x133x15毫米。它的设计初衷是为了能够无缝接入framework笔记本的

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器