YAF combined with user-defined view (template) engine Smarty (YAF + Smarty)

Source: Internet
Author: User

(1) Entry file:/public/index.php:

<?phpdefine ("DS", '/');d efine (' Application_path ', DirName (__file__). DS. ' ... '. DS);//point to the public directory at the top level: /$application = new Yaf_application (Application_path. "/conf/application.ini"); $application->bootstrap ()->run ();? >

(2) define the own view engine Smarty(/application/bootstrap.php ) in the bootstrapper bootstrap.php ):

Class Bootstrap extends yaf_bootstrap_abstract{public        function _initconfig () {                //save configuration                $arrConfig = Yaf _application::app ()->getconfig ();                Yaf_registry::set (' config ', $arrConfig);        }        Other definitions Ignore        ... Public Function _initsmarty (Yaf_dispatcher $dispatcher) {                 //init smarty view engine                  $smarty = new Smarty_adapter (NULL, yaf_registry::get ("config")->get ("Smarty"));                 $dispatcher->setview ($smarty);       }}

(3) Add smarty The adapter is smarty_adapter class, make y Af smarty view Do the functions implemented in Smarty_adapter class inside, yaf Span style= "font-family: Arial" > manipulate smarty

      First Download smarty source package (such as : smarty-2.6.29.tar.gz library below, and named smarty smarty new adapter.php file, where you add the following:

Vim adapter.php <?php/* ensure Smarty.class.php */yaf_loader::import under smarty/libs/("smarty/libs/smarty.class.php"); /* The base class directory is Library*/class Smarty_adapter implements Yaf_view_interface/*smarty_adapter class is the adapter between YAF and Smarty */{/** * Sm    Arty Object * @var Smarty */public $_smarty; /** * Constructor * * @param string $tmplPath * @param array $extraParams * @return void */Pub        Lic 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->_smar            Ty->template_dir = $path;        Return    } throw new Exception (' Invalid path provided ');    }/** * Retrieve the current template directory * * @return String */Public Function Getscriptpath ()    {return $this->_smarty->template_dir; }/** * Alias for Setscriptpath * * @param string $path * @param string $prefix Unused * @return Vo    ID */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);     }/** * 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 */Pub    Lic function __unset ($key) {$this->_smarty->clear_assign ($key); }/** * Assign variables to the template * * allows setting a specific key to the specified value, OR Passi     ng * 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-&gt            ; _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, $value = NULL) {return $this->_smarty->fetch ($name);    Public function display ($name, $value = NULL) {echo $this->_smarty->fetch ($name); }}?>

(4) Modify the application configuration file and add The configuration content of the Smarty section:

Vim Application.ini [common]application.directory = Application_path  "/application" Application.dispatcher.catchException = Trueapplication.bootstrap = Application_path "/application/bootstrap.php" Application.library = Application_path "/application/library" Application.baseuri = "; Application.dispatcher.defaultModule = Indexapplication.dispatcher.defaultController = Indexapplication.dispatcher.defaultAction = index;errors (see bootstrap::initerrors) application.showerrors=0[ smarty:common]application.view.ext= "TPL"  ;; Set the suffix of the view file to Tpl;smarty.left_delimiter   = "{"  ; Set the "{" condition when the template extracts the value; Smarty.right_delimiter  = "}}"  ; Smarty.template_dir     = Application_path "/application/views/" smarty.compile_dir      = Application_path "/ application/views/templates_c/"Smarty.cache_dir        = Application_path"/application/views/templates_d/"; smarty.caching = 0;; Smarty.cache_lifetime = 600; [Product:smarty]

(5) a simple MVC example based on YAF + Smarty:

controller (controllers): Add smarty.php Controller files under the Controllers directory

Vim smarty.php <?php class Smartycontroller extends Yaf_controller_abstract {public     function smartyaction ()     {         /* default Template_dir directory two/two.tpl*/         $this->getview ()->assign ("Content", "Hello hadoop! Welcome to Beijing!<br/> ");         /* Specify the template under the Template_dir directory */         $this->getview ()->display (' Smarty.tpl ');         /*false to suppress the default template   return False indicates that display specified template is displayed */         //return false;}      ? >

Views: render a Web page with a custom view engine (Smarty) , add the Smarty directory under the Views directory, Then add the Smarty.tpl file in the Smarty directory and add the Smarty.tpl file directly under the Views directory (test display method)

Vim smarty.tpl 
(6) Test: In the browser input: Http://172.16.2.33/smarty/smarty, the results show:

Hello hadoop!   Welcome to beijing! The default view file's storage directory, which is the views/smarty/directory
Hello hadoop!   Welcome to beijing! Storing the directory through the view file specified by display







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

YAF combined with user-defined view (template) engine Smarty (YAF + Smarty)

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.