Zend Framework实现Zend_View集成Smarty模板系统的方法,zend_viewsmarty
Zend Framework实现Zend_View集成Smarty模板系统的方法,zend_viewsmarty
本文实例讲述了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程序设计有所帮助。
您可能感兴趣的文章:
- Zend Framework教程之Zend_Controller_Plugin插件用法详解
- Zend Framework教程之响应对象的封装Zend_Controller_Response实例详解
- Zend Framework教程之请求对象的封装Zend_Controller_Request实例详解
- Zend Framework教程之动作的基类Zend_Controller_Action详解
- Zend Framework教程之分发器Zend_Controller_Dispatcher用法详解
- Zend Framework教程之前端控制器Zend_Controller_Front用法详解
- Zend Framework动作助手Redirector用法实例详解
- Zend Framework动作助手Url用法详解
- Zend Framework动作助手Json用法实例分析
- Zend Framework动作助手FlashMessenger用法详解
- Zend Framework创建自己的动作助手详解
- Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
- Zend Framework教程之路由功能Zend_Controller_Router详解

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

.NETFramework4是開發人員和最終使用者在Windows上執行最新版本的應用程式所必需的。但是,在下載安裝.NETFramework4時,許多用戶抱怨安裝程式在中途停止,顯示以下錯誤訊息-「 .NETFramework4hasnotbeeninstalledbecauseDownloadfailedwitherrorcode0x800c0006 」。在您的裝置上安裝.NETFramework4時,如果您也在體驗它,那麼您就來對了地方

每當您的Windows11或Windows10PC出現升級或更新問題時,您通常會看到一個錯誤代碼,指示故障背後的實際原因。但是,有時,升級或更新失敗可能不會顯示錯誤代碼,這時就會混淆。有了方便的錯誤代碼,您可以確切地知道問題出在哪裡,因此您可以嘗試修復。但是由於沒有出現錯誤代碼,因此識別問題並解決它變得極具挑戰性。這會佔用您大量時間來簡單地找出錯誤背後的原因。在這種情況下,您可以嘗試使用Microsoft提供的名為SetupDiag的專用工具,該工具可協助您輕鬆識別錯誤背後的真
![SCNotification 已停止運作 [修復它的 5 個步驟]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
身為Windows用戶,您很可能會在每次啟動電腦時遇到SCNotification已停止工作錯誤。 SCNotification.exe是一個微軟系統通知文件,由於權限錯誤和點網故障等原因,每次啟動PC時都會崩潰。此錯誤也以其問題事件名稱而聞名。因此,您可能不會將其視為SCNotification已停止工作,而是將其視為錯誤clr20r3。在本文中,我們將探討您需要採取的所有步驟來修復SCNotification已停止運作,以免它再次困擾您。什麼是SCNotification.e

Laravel是目前最受歡迎的PHP框架之一,其強大的視圖生成能力是令人印象深刻的一點。視圖是Web應用程式中展示給使用者的頁面或視覺元素,其中包含HTML、CSS和JavaScript等程式碼。 LaravelView允許開發者使用結構化的模板語言來建立網頁,同時透過控制器和路由產生相應的視圖。在本文中,我們將探討如何使用LaravelView產生視圖。一、什

已安裝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應用程式時可能會遇到問題。聽起來很熟悉?不過請不要驚

PHP實作框架:ZendFramework入門教學ZendFramework是PHP開發的開源網站框架,目前由ZendTechnologies維護,ZendFramework採用了MVC設計模式,提供了一系列可重複使用的程式碼庫,服務於實作Web2.0應用程式和Web服務。 ZendFramework深受PHP開發者的歡迎與推崇,擁有廣泛

如何在Zend框架中使用ACL(AccessControlList)進行權限控制導言:在一個Web應用程式中,權限控制是至關重要的功能。它可以確保使用者只能存取其有權存取的頁面和功能,並防止未經授權的存取。 Zend框架提供了一種方便的方法來實現權限控制,即使用ACL(AccessControlList)元件。本文將介紹如何在Zend框架中使用ACL
