_php example of Zend framework for realizing Zend_view integrated Smarty template system

Source: Internet
Author: User
Tags key string mixed smarty template zend zend framework

This paper illustrates the method of implementing Zend_view Integrated Smarty template system in Zend Framework. Share to everyone for your reference, specific as follows:

Zend_view abstracts the Zend_view_interface, allowing us to integrate different view solutions, such as integrating smarty. To use a different view system as a view in Zend, just implement the Zend_view_interface interface.

Interface definition for Zend_view_interface:

<?php/** * Interface class for Zend_view compatible template engine implementations * * @category Zend * @packag E Zend_view * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license http://fr AMEWORK.ZEND.COM/LICENSE/NEW-BSD New BSD License * * Interface Zend_view_interface {/** * return the template Engi Ne object, if any * * if using a third-party template engine, such as Smarty, Pattemplate, * phplib, etc., return The template engine object.
   Useful for calling * methods on this objects, such as for setting filters, modifiers, etc.
  * * @return Mixed */Public function getengine (); /** * Set The path to find the view script used by render () * * @param string|array the directory (-ies) to Set a s the path.
   Note this * The concrete view implentation may not necessarily support multiple * directories.
  * @return void */Public Function Setscriptpath ($path); /** * Retrieve all VieW Script Paths * * @return array */Public function getscriptpaths (); /** * Set A base path to all view resources * * @param string $path * @param string $classPrefix * @return
  void */Public Function Setbasepath ($path, $classPrefix = ' zend_view '); /** * ADD An additional path to view @param string $path * @param string $classPrefix * @retu
  RN void */Public Function Addbasepath ($path, $classPrefix = ' zend_view ');
   /** * Assign A variable to the view * @param string $key the variable name.
   * @param mixed $val the variable value.
  * @return void */Public Function __set ($key, $val); /** * Allows testing with empty () and isset () to Work * * @param string $key * @return Boolean/Public
  function __isset ($key); /** * allows unset () on the object properties to work * * @param string $key * @return void */Public functio
  n __unset ($key); /** * Assign variables to THe view script via differing strategies.  * * Suggested implementation is to allow setting a specific key to the * specified value, OR passing 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 PA
   IRS) * @param mixed $value (Optional) If Assigning a named variable, use this * as the value.
  * @return void */Public function assign ($spec, $value = null);
   /** * 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 ();
   /** * Processes A view script and returns the output.
   * * @param string $name The script name to process.
   * @return string the script output.
* * Public Function render ($name);

 }

The basic implementation of the integrated Smarty is as follows:

Smarty Download Address

Http://www.smarty.net/files/Smarty-3.1.7.tar.gz

Directory structure

root@coder-671t-m:/www/zf_demo1# Tree
.
├──application
│├──bootstrap.php
│├──configs
││└──application.ini
│├──controllers
││├──errorcontroller.php
││└──indexcontroller.php
│├──models
│└──views
│├──helpers
│└──scripts
│├──error
││└──error.phtml
│└──index
│├──index.phtml
│└──index.tpl
├──docs
│└──readme.txt
├──library
│├──lq
││└──view
││└──smarty.php
│└──smartylib
│├──debug.tpl
│├──plugins
││├── ............ .........
││└──variablefilter.htmlspecialchars.php
│├──smartybc.class.php
│├──smarty.class.php
│└──sysplugins
│├── ..................
│└──smarty_security.php
├──public
│└──index.php
├──temp
│└──smarty
│└──templates_c
│└──73d91bef3fca4e40520a7751bfdfb3e44b05bdbd.file.index.tpl.php
└──tests
├──application
│└──controllers
│└──indexcontrollertest.php
├──bootstrap.php
├──library
└──phpunit.xml

Directories, 134 files

/zf_demo1/library/lq/view/smarty.php

<?php require_once ' smartylib/smarty.class.php '; Class Lq_view_smarty implements Zend_view_interface {/** * Smarty Object * * @var Smarty/protected $_s
  Marty /** * Constructor * * @param $tmplPath String * @param $extraParams array * @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 () {Retu
  RN $this->_smarty;
   /** * Set the path to the templates * * @param $path String * The directory to Set as the path. * @return void/Public Function Setscriptpath ($path) {if (is_readable ($path)) {$this->_smarty-&gt
  ; template_dir = $path;    Return
  } 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 $path String * @param $prefix String * unused * @retur
  n void */Public Function Setbasepath ($path, $prefix = ' Zend_view ') {return $this->setscriptpath ($path); /** * Alias for Setscriptpath * * @param $path String * @param $prefix String * unused * @retu
  RN void */Public Function Addbasepath ($path, $prefix = ' Zend_view ') {return $this->setscriptpath ($path);
   /** * Assign A variable to the template * * @param $key String * The variable name.
   * @param $val Mixed * The variable value. * @return void */Public Function __set ($key, $val) {$this->_smarty->assign ($keY, $val);
   /** * Retrieve an assigned variable * * @param $key String * 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 $key String * @return Boolean/Pub
  Lic function __isset ($key) {return (null!== $this->_smarty->get_template_vars ($key)); /** * allows unset () on the object properties to work * * @param $key String * @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 $spec String|array * The assignment strategy to use (key or array of key * => value pairs) * @param $value Mixed * (Optional) If Assigning a named variable, use this * as the value. * @return void */Public function assign ($spec, $value = null) {if (Is_array ($spec)) {$this->_smart
      Y->assign ($SPEC);
    Return
  $this->_smarty->assign ($spec, $value);  }/** * Clear all assigned variables * * Clears all variables assigned to Zend_view either via {@link assign ()}
   The 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 $name String * The template to process.
   * @return string the output.
    * * Public Function render ($name) {Ob_start ();
    echo $this->_smarty->fetch ($name);
  Unset ($name);

 }
}

/zf_demo1/application/configs/application.ini

[Production]
Includepaths.library = Application_path "/... /library "
Bootstrap.path = Application_path"/bootstrap.php "bootstrap.class
=" Bootstrap "
appnamespace = "Application"
AUTOLOADERNAMESPACES.LQ = "Lq_"
pluginpaths. Lq_view_smarty = "Lq/view/smarty"
resources.frontController.controllerDirectory = Application_path "/ Controllers "
resources.frontController.params.displayExceptions = 1
phpsettings.display_startup_errors = 1
phpsettings.display_errors = 1

/zf_demo1/application/bootstrap.php

<?php
class Bootstrap extends Zend_application_bootstrap_bootstrap {
  /**
   * Initialize Smarty View
   */
  protected function _initsmarty () {
    $smarty = new Lq_view_smarty ();
    $smarty->setscriptpath ('/www/zf_demo1/application/views/scripts ');
    return $smarty;
  }


/zf_demo1/application/controllers/indexcontroller.php

<?php
class Indexcontroller extends Zend_controller_action {public
  function init () {
    /
     * * Initialize Action Controller Here
     *
  /} public
  function Indexaction () {
    $this->_helper-> Gethelper (' Viewrenderer ')->setnorender ();
    $this->view = $this->getinvokearg (' Bootstrap ')->getresource (' Smarty ');
    $this->view->book = ' Hello world! ';
    $this->view->author = ' by Smarty ';
    $this->view->render (' Index/index.tpl ');
  }


/zf_demo1/application/views/scripts/index/index.tpl

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
< html>
 
 

If you need to configure Smarty you can open the/zf_demo1/library/smartylib/smarty.class.php file for the appropriate configuration, for example

/** * Initialize New Smarty Object * */Public Function __construct () {//Selfpointe
    R needed by some the other class methods $this->smarty = $this;
    if (is_callable (' mb_internal_encoding ')) {mb_internal_encoding (smarty::$_charset);
    } $this->start_time = Microtime (true); Set Default dirs $this->settemplatedir ('/www/zf_demo1/temp/smarty '). Ds. ' Templates '. DS)->setcompiledir ('/www/zf_demo1/temp/smarty '). Ds. ' Templates_c '. DS)->setpluginsdir (smarty_plugins_dir)->setcachedir ('/www/zf_demo1/temp/smarty '). Ds. ' Cache '. DS)->setconfigdir ('/www/zf_demo1/temp/smarty '). Ds. ' Configs '.
    DS); $this->debug_tpl = ' file: '. DirName (__file__).
    '/debug.tpl ';
    if (Isset ($_server[' script_name ')) {$this->assignglobal (' script_name ', $_server[' script_name ']); }
}

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.

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.