欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Zend Framework实现Zend_View集成Smarty模板系统的方法

程序员文章站 2023-12-10 08:06:34
本文实例讲述了zend framework实现zend_view集成smarty模板系统的方法。分享给大家供大家参考,具体如下: zend_view抽象出了zend_vi...

本文实例讲述了zend framework实现zend_view集成smarty模板系统的方法。分享给大家供大家参考,具体如下:

zend_view抽象出了zend_view_interface,可以让我们集成不同的视图解决方案,例如可以集成smarty。要在zend中使用其他视图系统作为视图,只要实现zend_view_interface接口即可。

zend_view_interface的接口定义:

<?php
/**
 * interface class for zend_view compatible template engine implementations
 *
 * @category  zend
 * @package  zend_view
 * @copyright copyright (c) 2005-2011 zend technologies usa inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   new bsd license
 */
interface zend_view_interface
{
  /**
   * return the template engine 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 these 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 as the path. note that
   * 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 resources
   *
   * @param string $path
   * @param string $classprefix
   * @return 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 object properties to work
   *
   * @param string $key
   * @return void
   */
  public function __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 pairs)
   * @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);
}

集成smarty的基本实现如下:

smarty下载地址

http://www.smarty.net/files/smarty-3.1.7.tar.gz

目录结构

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

24 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 $_smarty;
  /**
   * constructor
   *
   * @param $tmplpath string
   * @param $extraparams array
   * @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 $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->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
   * @return void
   */
  public function setbasepath($path, $prefix = 'zend_view') {
    return $this->setscriptpath ( $path );
  }
  /**
   * alias for setscriptpath
   *
   * @param $path string
   * @param $prefix string
   *      unused
   * @return 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
   */
  public function __isset($key) {
    return (null !== $this->_smarty->get_template_vars ( $key ));
  }
  /**
   * allows unset() on object properties to work
   *
   * @param $key string
   * @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 $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->_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 $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>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>
{$book}
{$author}
</body>
</html>

如果需要配置smarty可以打开/zf_demo1/library/smartylib/smarty.class.php文件进行相应配置例如

/**
* initialize new smarty object
*
*/
public function __construct()
{
    // selfpointer needed by some 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']);
    }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。