Zend Framework Framework Smarty Extension implementation method _php instance

Source: Internet
Author: User
Tags smarty template zend framework
This paper describes the implementation method of Zend Framework Framework smarty Extension. Share to everyone for your reference, as follows:

Today, we summarize the method of extending the Smarty template in the ZF framework, which is described in more detail in the ZF Help documentation, which I'll say a little bit more about.

One. Place the Smarty core package in the Lib folder and include it in the package (internals/,plugins/,config_file.class.php,smarty.class.php,smarty_ COMPILER.CLASS.PHP,DEBUG.TPL).

Two. Add file under Zend/view: smarty.php, the contents of the file are as follows:

<?php/*** zend_view_interface*/require_once ' zend/view/interface.php ';/*** Smarty */require_once ("smarty/  Smarty.class.php ")/*** create 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 $t  his->_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 (' Invalid path provided '); }/** * Set smarty Cache * @author Lengfeng */Public Function Setcompilepath ($path) {if (is_readable ($path)) {$      This->_smarty->compile_dir = $path;    Return      } throw new Exception (' Invalid path provided ');      }/** * Set Smarty compiled document * @author Lengfeng */Public Function Setcachepath ($path) {if (is_readable ($path)) {      $this->_smarty->cache_dir = $path;    Return      } throw new Exception (' Invalid path provided '); }/** * Retrieve the current template directory * * @return String */Public Function getscriptpaths () {RET  Urn Array ($this->_smarty->template_dir); }/** * Alias for Setscriptpath * * @param string $path * @param string $prefix Unused * @return void */Pub  Lic 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, $pre  fix = ' 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 fun  Ction __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 AR   Ray * 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); }/** * Sets whether to generate cache * If there are no parameters, the default is true */Public function Setcache ($bool) {if (Isset ($bool)) {$this->_smar      ty->caching = $bool;    Return }  }}

Three. Under the App folder, create the cache, compile folder

Four. In the Config.ini configuration file, add

Dir.compile    =.. /app/compiledir.cache    =.. /app/cache

Three or 42 steps can be found in the previous tutorial on Zendfreamwork Framework Building website

Five. Add in the application.php file

/*** Initialize 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 = "<{"; The definition identifier    $smarty->right_delimiter = "}>";    $registry = Zend_registry::getinstance ();    $registry->set (' Smartyview ', $smarty); Smarty Object    $registry->set (' Sview ', $view);          }

and add in the function init ()

$this->_initsmartyview ();

Six. Call in controller

Because the object has already been registered, it can be called as follows:

$view = Zend_registry::getinstance ()->get ("Smartyview");//Note that this is a Smarty object, using 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 ZF methods without changing. In this way, if you want to change the previously written code to use Smaty, the background does not change, just need to changes the view file on the line

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

  • 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.