Build a website using the Zend framework (III)-Smarty Extension

Source: Internet
Author: User
Tags smarty template zend framework
Build a website using the Zend framework (iii) -- smarty extension 2008/07/24 p.m.
Today, I will summarize the methods for extending the smarty template in the ZF framework. I have provided a detailed introduction in the ZF help document. Here I will talk a little more about it.



I. Put the core package of smarty In the Lib folder, which should include (internals/, plugins/, config_file.class.php, smarty. Class. php, smarty_compiler.class.php, debug. TPL ).


2. Add the file smarty. php under Zend/view. The file content is as follows:



<? PHP

/**

* Zend_view_interface

*/

Require_once 'zend/View/interface. php ';


/**

* Smarty

*/

Require_once ("Smarty/smarty. Class. php ");


/**

* Create a smarty View

*/

Class zend_view_smarty 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;

Return;

}


Throw new exception ('invalidpath provided ');

}

/**

* Set smarty Cache

* @ Author Lengfeng

*/

Public Function setcompilepath ($ PATH ){

If (is_readable ($ PATH )){

$ This-> _ smarty-> compile_dir = $ path;

Return;

}


Throw new exception ('invalidpath provided ');

}

/**

* Set smarty compiled documents

* @ Author Lengfeng

*/

Public Function setcachepath ($ PATH ){

If (is_readable ($ 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 );

}

/**

* Set whether to generate a cache.

* If no parameter exists, the default value is true.

*/

Public Function setcache ($ bool ){

If (isset ($ bool )){

$ This-> _ smarty-> caching = $ bool;

Return;

}

}

}


3. Create a cache and compile folder in the app folder


4. Add the following content to the config. ini configuration file:

Dir. Compile = ../APP/compile

Dir. cache = ../APP/Cache

Step 3 and Step 4 can be found in steps (2).


5. Add


/**

* Initialize the smarty view.

*

*/

Private function _ initsmartyview ()

{

$ View = new zend_view_smarty ();

$ View-> setbasepath ($ this-> _ pathconfig-> Dir-> viewbase );

$ View-> setscriptpath ($ this-> _ pathconfig-> Dir-> viewbase. "/scripts ");

$ View-> setcompilepath ($ this-> _ pathconfig-> Dir-> compile );

$ View-> setcachepath ($ this-> _ pathconfig-> Dir-> cache );

$ Smarty = $ view-> getengine ();

$ Smarty-> caching = false;

$ Smarty-> debugging = true;

$ Smarty-> compile_check = true;

$ Smarty-> left_delimiter = "<{"; // defines the identifier

$ Smarty-> right_delimiter = "}> ";

$ Registry = zend_registry: getinstance ();

$ Registry-> set ('smartyview', $ smarty); // smarty object

$ Registry-> set ('sview', $ view );

}

Add $ this-> _ initsmartyview () to the init () function ();

Vi. Calling in Controller

Because the object has been registered, you can call it as follows:


$ View = zend_registry: getinstance ()-> get ("Smartyview"); // note that this is a smarty object, using the smarty syntax, such as $ view-> assign ("user", "root ");

$ View = zend_registry: getinstance ()-> get ("Sview");
// This Is The ZF view object, which is used by the methods in ZF and does not need to be changed. In this way, if you want to change the previously written code to smaty,
You don't need to change the background. You just need to change the view file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.