ZF is used for the project in the past two days.
1.7. For various reasons, the project view must be completed using smarty. There is a lot of information on the Internet about combining ZF with smarty. When you use smarty and find a problem
ZF's various view assistants cannot be used. I have been online for a long time and have not seen any materials. Looking at the ZF source code, I finally found a solution. Record here:
1. Integrate smarty
1. First, you need to write the ZF smarty View Class Based on the zend_view_interface interface. Assume that you create
Library/View/. It is assumed that the smarty class library you downloaded is also in the library directory. Create under library/View/
Smarty. php, and then write the zend_view_smarty class to implement the zend_view_interface interface according to the instructions in the manual. The Code is as follows:
<? PHP
/**
* Implement an interface for Zend View
* Author: xydream@gmail.com
*/
Require_once 'zend/View/interface. php ';
Require_once 'smarty/smarty. Class. php ';
Class zend_view_smarty extends zend_view_abstract 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;
}
}
/**
* Returns the template engine object.
* @ Return smarty
*/
Public Function getengine ()
{
Return $ this-> _ smarty;
}
/**
* Set the path for storing the template file.
* @ Param string $ PATH refers to the directory of the path.
* @ Return void
*/
Public Function setscriptpath ($ PATH)
{
If (is_readable ($ PATH )){
$ This-> _ smarty-> template_dir = $ path;
Return;
}
Throw new exception ('invalidpath provided ');
}
/**
* Return the path of the current template file.
* @ Return string
*/
Public Function getscriptpaths ()
{
Return array (
$ This-> _ smarty-> template_dir );
}
/**
* Configure the smarty Engine
*
* @ Param unknown_type $ VaR
* @ Param unknown_type $ Value
*/
Public Function setupsmarty ($ var, $ value = '')
{
If (is_array ($ var )){
Foreach ($ VaR as $ k => $ v ){
$ This-> setupsmarty ($ K, $ V );
}
} Else {
If (in_array ($ var, array (
'Template _ dir ',
'Compile _ dir ',
'Cache _ dir ',
'Caching ',
'Cache _ lifetime ',
'Left _ delimiter ',
'Right _ delimiter '))){
$ This-> _ smarty-> $ Var = $ value;
}
}
Return;
}
/**
* Get the smarty Configuration
*
* @ Param unknown_type $ VaR
* @ Return unknown
*/
Public Function getsmartyset ($ var)
{
If (in_array ($ var, array (
'Template _ dir ',
'Compile _ dir ',
'Cache _ dir ',
'Caching ',
'Cache _ lifetime ',
'Left _ delimiter ',
'Right _ delimiter '))){
Return $ this-> _ smarty-> $ var;
}
}
/**
* Set the path for storing the template compilation File
* @ Param string $ PATH refers to the directory of the path.
* @ Return void
*/
Public Function setcompilepath ($ PATH)
{
If (is_readable ($ PATH )){
$ This-> _ smarty-> compile_dir = $ path;
Return;
}
Throw new exception ('invalidpath provided ');
}
/**
* Return to the path where the template compilation file is currently stored.
* @ Return string
*/
Public Function getcompilepaths ()
{
Return array (
$ This-> _ smarty-> compile_dir );
}
/**
* Set the path for storing the template cache file
* @ Param string $ PATH refers to the directory of the path.
* @ Return void
*/
Public Function setcachepath ($ PATH)
{
If (is_readable ($ PATH )){
$ This-> _ smarty-> cache_dir = $ path;
Return;
}
Throw new exception ('invalidpath provided ');
}
/**
* Returns the path of the cache file for the current template.
* @ Return string
*/
Public Function getcachepaths ()
{
Return array (
$ This-> _ smarty-> cache_dir );
}
/**
* The alias of the setscriptpath Method
* @ Param string $ path
* @ Param string $ prefix unused
* @ Return void
*/
Public Function setbasepath ($ path, $ prefix = 'zend _ view ')
{
Return $ this-> setscriptpath ($ PATH );
}
/**
* The alias of the setscriptpath Method
* @ Param string $ path
* @ Param string $ prefix unused
* @ Return void
*/
Public Function addbasepath ($ path, $ prefix = 'zend _ view ')
{
Return $ this-> setscriptpath ($ PATH );
}
/**
* Set a variable for the template.
* @ Param string $ key variable name
* @ Param mixed $ Val variable value
* @ Return void
*/
Public Function _ set ($ key, $ Val)
{
$ This-> _ smarty-> assign ($ key, $ Val );
}
/**
* Return a set template variable value.
* @ Param string $ key variable name
* @ Return value of the Mixed Variable
*/
Public Function _ Get ($ key)
{
Return $ this-> _ smarty-> get_template_vars ($ key );
}
/**
* Empty () and isset () can be used for testing.
* @ Param string $ key
* @ Return Boolean
*/
Public Function _ isset ($ key)
{
Return (null! ==$ This-> _ smarty-> get_template_vars ($ key ));
}
/**
* Allow unset () attributes of an object
* @ Param string $ key
* @ Return void
*/
Public Function _ unset ($ key)
{
$ This-> _ smarty-> clear_assign ($ key );
}
/**
* Set a variable for the template.
* You can set a specific value to a specific variable or pass an array and assign values in batches using key => value pairs. ()
* @ See _ set ()
* @ Param string | array $ spec variable name, or Array
* @ Param mixed $ value is optional. If the previous parameter is a variable, this value is assigned to the variable.
* @ Return void
*/
Public Function assign ($ spec, $ value = NULL)
{
If (is_array ($ spec )){
$ This-> _ smarty-> assign ($ spec );
Return;
}
$ This-> _ smarty-> assign ($ spec, $ value );
}
/**
* Clear all configured Variables
* @ Return void
*/
Public Function clearvars ()
{
$ This-> _ smarty-> clear_all_assign ();
}
/**
* Process a template and return its output ..
* @ Param string $ name the template to be processed.
* @ Return string output content.
*/
Public Function render ($ name)
{
Return $ this-> _ smarty-> fetch ($ name );
}
Protected function _ run ()
{
Include func_get_arg (0 );
}
}
2. Next, we need to write an action assistant, register smarty to the global properties of the controller, and write the class zend_controller_action_helper_view. The corresponding directory structure is in the library directory.
Zend/controller/Action/helper/view. php. The Code is as follows:
<? PHP
Class zend_controller_action_helper_view extends zend_controller_action_helper_abstract
{
Protected $ _ smartyconfig;
Protected $ _ tmppath;
Public Function _ construct ($ tmppath, array $ config ){
$ This-> _ smartyconfig = $ config;
$ This-> _ tmppath = $ tmppath;
}
Public Function predispatch ()
{
$ Smarty = new zend_view_smarty ($ this-> _ tmppath, $ this-> _ smartyconfig );
$ This-> getactioncontroller ()-> View = $ smarty;
}
}
3. Add the action assistant and configure the smarty before the front-end controller is distributed in the entry file. Here I put the configuration in the configuration file, the specific code:
// Configure the Template
$ Style = empty ($ _ cookie ['style'])? $ Config-> smarty-> template_dir: $ _ cookie ['style'];
$ Viewbasepath = $ config-> smarty-> template_base_dir;
$ Templates_dir = $ viewbasepath. directory_separator. ltrim ($ style ,'///');
If (! Is_readable ($ templates_dir )){
Setcookie ('style', false, 315554400, // strtotime ('2017-01-01 '),
$ Config-> cookie-> path,
$ Config-> cookie-> domain,
$ Config-> cookie-> secure
);
Echo 'the style you selected does not exist! Call the default style to display ';
Exit ();
}
$ Smartysetup = array (
'Compile _ dir' => $ viewbasepath. $ config-> smarty-> compile_dir,
'Cache _ dir' => $ viewbasepath. $ config-> smarty-> cache_dir,
'Caching' => $ config-> smarty-> caching,
'Cache _ lifetime' => $ config-> smarty-> cache_lifetime,
'Left _ delimiter '=> $ config-> smarty-> left_delimiter,
'Right _ delimiter '=> $ config-> smarty-> right_delimiter );
Zend_controller_action_helperbroker: addhelper (New zend_controller_action_helper_view ($ templates_dir, $ smartysetup ));
This completes the integration of ZF and smarty.
2. Allow smarty to use the zend_view assistant function (you need to modify the smarty source file)
1. Add an attribute for the class in smarty. Class. php: var $ _ zend_view = NULL;
2. Find This function
Function smarty () and then changed
Function smarty ($ zend_view = NULL)
Add the following statement internally:
$ This-> _ zend_view = $ zend_view;
3. Add a function
/**
* Added to use zend_view so that smarty can automatically call the helper class.
*
*/
Public Function _ call ($ name, $ ARGs)
{
Return $ this-> _ zend_view->__ call ($ name, $ ARGs );
}
4. Implementation class in zend_view_interface
In smarty. php
$ This-> _ smarty = new smarty (); changed:
$ This-> _ smarty = new smarty (New zend_view ());
In this way, you can use the <{PHP}> echo $ this-> URL (Array ('page' => $ page) in the template; <{/PHP}>
The helper class is used in this form.
From: http://hi.baidu.com/xydream/blog/item/e079dfef34161c3eacafd598.html