6.6 escaping output)
After the View Script obtains the variable, it needs to be output through escaping to convert it into HTML code that can be displayed on the page.
Output statement format:
Echo $ this-> escape ($ this-> variable );
$ Variable variables are passed in the View Script using the render method.
Generally, the passed variables are escaped through the htmlspecialchars () function of PHP. We can also implement our own escape functions. Refer to the above "Use callback function" example.
6.7 View Script Template system-operate a phplib Template
The template system further perfectly realizes the separation of view and program logic. View scripts can operate on templates of phplib and other types perfectly.
6.7.1install and call phplib
To test the following example, we must install the phplib template System in our environment. Download from the Internet to the phplib-7.4.ZIP installation package, decompress to install Zend library folder, the installation is complete.
In order to call the template class file in ZF's View Script, you must add the phplib template class library folder phplib-7.4/PHP to the search path in the set_include_path section of the boot file index. php. The following example also contains the search path of the class library file of the smarty template engine:
Set_include_path ('.'.
Path_separator. '../library /'.
Path_separator. '.../library/phplib-7.4/PHP /'.
Path_separator. '../library/smarty-2.6.19/libs /'.
Path_separator. 'models /'.
Path_separator. get_include_path ()
);
Note that all paths are based on the folder where the boot file is located. Although the folder in the View File is not the root directory of the boot file, the view file contains the statement include_once 'template of the phplib class library file. INC '; still uses the directory where the boot file is located as a reference.
6.7.2 call the phplib template in the View File
First, it contains the phplib class library file, and then declares an instance of the template class. To use the template class, you must first specify some attributes, such as the path of the specified template and the template file. Then, use set_var to pass the template variable, and finally call the template file using the parse method. For details about how to use the phplib template system, see the help documentation.
Example:
<? PHP
Include_once 'template. inc ';
$ TPL = new template ();
$ TPL-> set_root ('view ');
If ($ this-> books)
{
$ TPL-> set_file (Array (
"Booklist" => "Booklist. TPL ",
"Eachbook" => "eachbook. TPL ",
));
Foreach ($ this-> books as $ key => $ Val)
{
$ TPL-> set_var ('author', $ this-> escape ($ Val ['author']);
$ TPL-> set_var ('title', $ this-> escape ($ Val ['title']);
$ TPL-> parse ("books", "eachbook", true );
}
$ TPL-> pparse ("output", "Booklist ");
}
Else
{
$ TPL-> setfile ("nobooks", "nobooks. TPL ");
$ TPL-> pparse ("output", "nobooks ");
}
?>
Booklist. TPL file content:
<? PHP
If ($ this-> books ):
?>
<Table border = 1>
<Tr>
<TH> author </Th>
<TH> title </Th>
</Tr>
<? PHP
Foreach ($ this-> books as $ key => $ Val ):
?>
<Tr>
<TD> <? PHP echo $ this-> escape ($ Val ['author'])?> </TD>
<TD> <? PHP echo $ this-> escape ($ Val ['title'])?> </TD>
</Tr>
<? PHP endforeach;?>
</Table>
<? PHP
Else:
?>
<P> There are no books to display. </P>
<? PHP
Endif;
Eachbook. TPL file content:
<! -- Eachbook. TPL -->
<Tr>
<TD> {author} </TD>
<TD> {Title} </TD>
</Tr>
6.8 View Script Template system-use zend_view_interface to call a third-party template engine
We can also implement the zend_view_interface interface to obtain an available template system.
ZF's original definition of the zend_view_interface interface:
/** Return the actual template engine object */
Public Function getengine ();
/* Set the path to view scripts/templates */
Public Function setscriptpath ($ PATH );
/* Set a base path to all view resources */
Public Function setbasepath ($ path, $ prefix = 'zend _ view ');
/* Add an additional base path to view resources */
Public Function addbasepath ($ path, $ prefix = 'zend _ view ');
/* Retrieve the current script paths */
Public Function getscriptpaths ();
/* Overloading methods for assigning template variables as object properties */
Public Function _ set ($ key, $ value );
Public Function _ Get ($ key );
Public Function _ isset ($ key );
Public Function _ unset ($ key );
/* Manual assignment of template variables, or ability to assign multiple
Variables en masse .*/
Public Function assign ($ spec, $ value = NULL );
/* Unset all assigned template variables */
Public Function clearvars ();
/* Render the template named $ name */
Public Function render ($ name );
With this interface, we can easily wrap a third-party template engine, such as smarty, into a template class compatible with zend_view.
6.8.1install smarty
Download the smarty software package and decompress it to the Zend library folder to complete the installation.
To obtain the template class file by calling the View Script in ZF, you must go to the index of the boot file. in the set_include_path section of PHP, add the smarty template class library folder smarty-2.6.19/libs to the search path. See the preceding phplib installation instructions.
The smarty template engine must create the template_dir and compile_dir folders to work. The examples in the ZF Manual cannot be run because these settings are missing. The correct code snippets are as follows:
Public Function setscriptpath ($ PATH)
{
If (is_readable ($ PATH ))
{
$ This-> _ smarty-> template_dir = $ path;
$ This-> _ smarty-> compile_dir = $ path; // a required statement is used to set the compilation path.
$ This-> _ smarty-> cache_dir = $ path; // you can specify a cache path.
Return;
}
......
We put the implementation class of the zend_view_interface interface in the zendviewsmarty. php file in the models folder. The content of this file is as follows:
<? PHP
Require_once 'zend/View/interface. php ';
Require_once 'smarty. Class. php ';
Class zendviewsmarty 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;
$ This-> _ smarty-> compile_dir = $ path;
$ This-> _ smarty-> cache_dir = $ path;
Return;
}
Throw new exception ('invalidpath 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 );
}
}
?>
The call code for our template class in the control script:
Function smartyaction ()
{
$ View = new zendviewsmarty (); // create a new template class by instance
$ View-> setscriptpath ('view'); // you can specify the template file path.
$ View-> book = 'enter Zend Framework Programme '; // pass variables to the template engine
$ View-> author = 'zhang Qing (mesh )';
Echo $ view-> render ('bookinfo. TPL '); // view rendering
}
We can see that, due to the good features of the smarty template engine, the code for implementing the above interfaces is more complicated, and the control code here is much simpler than the phplib template. We do not need to pass the data to the View Script like the phplib engine. Then, the View Script declares the phplib class and sends the data to the template for presentation. The data is directly transmitted to the smarty template in the control script for presentation. This is because zendviewsmarty implements the zend_view interface, which has the same usage as zend_view.
Note: This example is only one of the methods for using the smarty engine. In ZF, you can also use the smarty template engine in other forms. We will introduce it in other chapters.