Yaf_View_Interface of the 14-Yaf built-in class

Source: Internet
Author: User
Yaf provides users with a scalable and customizable view engine interface, that is, Yaf_View_Interface, yaf also has built-in implementation of a simple template engine Yaf_View_Simple Yaf to provide users with a scalable and customizable view engine interface, that is, Yaf_View_Interface, yaf also has built-in implementation of a simple template engine Yaf_View_Simple, which only supports PHP scripts. Therefore, we generally use our own view engine (such as Smarty) to replace Yaf_View_Simple built in Yaf.

The following uses the Smarty engine as an example to illustrate how to implement the Yaf_View_Interface. Before implementing the interface, let's take a look at all the member methods that Yaf_View_Interface needs to implement.


Yaf_View_Interface {/* method */abstract public bool assign (string $ name [, string $ value]) abstract public bool display (string $ tpl [, array $ tpl_vars]) abstract public void getScriptPath (void) abstract public string render (string $ tpl [, array $ tpl_vars]) abstract public void setScriptPath (string $ template_dir )} // therefore, we must implement the above five methods. The following is a code example.
 _ Smarty = new Smarty; if (null! ==$ TmplPath) {$ this-> setScriptPath ($ tmplPath);} foreach ($ extraParams as $ key => $ value) {$ this-> _ smarty-> $ key = $ value ;}} /*** 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 ');} /*** 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 );} /*** 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 | ar Ray $ 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 Ssigned to Yaf_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, $ value = NULL) {return $ this-> _ smarty-> Fetch ($ name);} public function display ($ name, $ value = NULL) {echo $ this-> _ smarty-> fetch ($ name) ;}}?> // After implementation, we can register our smarty template engine in Bootstrap.
 GetConfig (); Yaf_Registry: set ("config", $ config);} public function _ initLocalName () {/** register Smarty_Adapter as a local class crying */Yaf_Loader :: getInstance ()-> registerLocalNamespace ('smarty ');} public function _ initSmarty (Yaf_Dispatcher $ dispatcher) {$ Smarty = new Smarty_Adapter (null, Yaf_Registry: get ("config ") -> get ("smarty"); $ dispatcher-> setView ($ smarty);/* set smarty as our template engine */}?>

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.