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 */}?>