本文实例讲述了Zend Framework实现Zend_View集成Smarty模板系统的方法。分享给大家供大家参考,具体如下: Zend_View抽象出了Zend_View_Interface,可以让我们集成不同的视图解决方案,例如可以集成smarty。要在zend中使用其他视图系统作为视图,只要实现Zen
本文实例讲述了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
<?php require_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 = 1 phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1
/zf_demo1/application/Bootstrap.php
<?php class 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
<?php class 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']); } }
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

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

每当您的Windows11或Windows10PC出现升级或更新问题时,您通常会看到一个错误代码,指示故障背后的实际原因。但是,有时,升级或更新失败可能不会显示错误代码,这时就会出现混淆。有了方便的错误代码,您就可以确切地知道问题出在哪里,因此您可以尝试修复。但是由于没有出现错误代码,因此识别问题并解决它变得极具挑战性。这会占用您大量时间来简单地找出错误背后的原因。在这种情况下,您可以尝试使用Microsoft提供的名为SetupDiag的专用工具,该工具可帮助您轻松识别错误背后的真
![SCNotification 已停止工作 [修复它的 5 个步骤]](https://img.php.cn/upload/article/000/887/227/168433050522031.png)
作为Windows用户,您很可能会在每次启动计算机时遇到SCNotification已停止工作错误。SCNotification.exe是一个微软系统通知文件,由于权限错误和点网故障等原因,每次启动PC时都会崩溃。此错误也以其问题事件名称而闻名。因此,您可能不会将其视为SCNotification已停止工作,而是将其视为错误clr20r3。在本文中,我们将探讨您需要采取的所有步骤来修复SCNotification已停止工作,以免它再次困扰您。什么是SCNotification.e

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

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

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

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

随着物联网技术的发展和普及,越来越多的应用场景需要使用PHP语言进行物联网开发。PHP作为一种广泛应用于Web开发的脚本语言,它的易学易用、开发速度快、可扩展性强等特点,使其成为开发物联网应用的一种优秀选择。本文将介绍在PHP中实现物联网开发的常用技术和方法。一、传输协议和数据格式物联网设备通常使用TCP/IP或UDP协议进行数据传输,而HTTP协议是一个优


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!