_php Example of Smarty Extension implementation method for Zend framework frame

Source: Internet
Author: User
Tags smarty template zend zend framework

This paper illustrates the Smarty extension implementation method of Zend Framework frame. Share to everyone for your reference, specific as follows:

Today, we summarize the method of extending the Smarty template in the ZF framework, and I'll say a little more about it in the ZF help documentation.

First, place the Smarty core packages under the Lib folder and include them in the package (internals/,plugins/,config_file.class.php,smarty.class.php,smarty_ COMPILER.CLASS.PHP,DEBUG.TPL).

Two. Add the file under Zend/view: smarty.php, the contents of the document 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 fun
    Ction __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;
  } throw new Exception (' Invalid path provided ');
      /** * Set smarty Cache * @author Lengfeng/Public Function Setcompilepath ($path) {if (is_readable ($path)) {
      $this->_smarty->compile_dir = $path;
  } 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;
  } throw new Exception (' Invalid path 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/Pub Lic function __isset ($key) {return (null!== $this->_smarty->get_template_vars ($key)); /** * allows unset () on the object properties to work * * @param string $key * @return void */Public fun
  Ction __unset ($key) {$this->_smarty->clear_assign ($key); }/** * Assign variables to the template * * allows setting a specific key to the specified value, OR passing a
   N 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 PA
   IRS) * @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);
  $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); /** * Settings Generate cache * If there are no parameters, the default is true */Public function Setcache ($bool) {if (Isset ($bool)) {$this-&G
      t;_smarty->caching = $bool;


Three. Create cache, compile folder under App folder

Four. Add in Config.ini configuration file

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

Three, 42 steps can be seen in front of the zendfreamwork framework to build a website related tutorials

Five. Add in 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->debugging = true;
    $smarty->compile_check = true;    
    $smarty->left_delimiter = "<{"; Definition designator
    $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 been registered, it can be called as follows:

$view = Zend_registry::getinstance ()->get ("Smartyview");
Note that this is the Smarty object, using Smarty syntax such as $view->assign ("User", "root");
$view = Zend_registry::getinstance ()->get ("Sview"); 
This is the ZF view object, in the way of the ZF, without change.
//In this way, if you want to change the previously written code to use Smaty, the background does not change, just want to change the view file on the line

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend 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.