This paper describes the method of Zend Framework to implement Zend_view integrated Smarty template system. Share to everyone for your reference, as follows:
Zend_view abstracts out the zend_view_interface, allowing us to integrate different view solutions, such as the integration of Smarty. To use a different view system as a view in Zend, just implement the Zend_view_interface interface.
Interface definitions for Zend_view_interface:
<?php/** * Interface class for Zend_view compatible template engine implementations * * @category Zend * @package Zen D_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 eng Ine object. Useful for calling * methods in 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 T He path. Note that * The concrete view implentation is 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 Vo ID */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 functi On __isset ($key); /** * allows unset () on object properties to work * * @param string $key * @return void */Public function __un Set ($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 * ma Sse. * * @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);}
The basic implementation of the integrated Smarty is as follows:
Smarty Download Address
Http://www.smarty.net/files/Smarty-3.1.7.tar.gz
Directory structure
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
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 ob ject * * @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->tem Plate_dir = $path; Return } throw new Exception (' INvalid path provided '); }/** * Retrieve the current template directory * * @return String */Public Function getscriptpaths () {Retu RN 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 fun Ction __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 AR Ray * of key = value pairs to set en masse. * * @see __set () * @param $spec String|array * The assignment strategy to use (key or array of key * =&G T 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
Insert Title here{$book} {$author}
If you need to configure Smarty, you can open the/zf_demo1/library/smartylib/smarty.class.php file for appropriate configuration such as
/*** 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 ']); }}
More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
I hope this article is helpful to you in PHP programming.