php封装的smartyBC类完整实例


Posted in PHP onOctober 19, 2016

本文实例讲述了php封装的smartyBC类。分享给大家供大家参考,具体如下:

<?php
/**
 * Project:   Smarty: the PHP compiling template engine
 * File:    SmartyBC.class.php
 * SVN:     $Id: $
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * For questions, help, comments, discussion, etc., please join the
 * Smarty mailing list. Send a blank e-mail to
 * smarty-discussion-subscribe@googlegroups.com
 *
 * @link   http://www.smarty.net/
 * @copyright 2008 New Digital Group, Inc.
 * @author  Monte Ohrt <monte at ohrt dot com>
 * @author  Uwe Tews
 * @author  Rodney Rehm
 * @package  Smarty
 */
/**
 * @ignore
 */
require_once(dirname(__FILE__) . '/Smarty.class.php');
/**
 * Smarty Backward Compatability Wrapper Class
 *
 * @package Smarty
 */
class SmartyBC extends Smarty
{
  /**
   * Smarty 2 BC
   *
   * @var string
   */
  public $_version = self::SMARTY_VERSION;
  /**
   * Initialize new SmartyBC object
   *
   * @param array $options options to set during initialization, e.g. array( 'forceCompile' => false )
   */
  public function __construct(array $options = array())
  {
    parent::__construct($options);
    // register {php} tag
    $this->registerPlugin('block', 'php', 'smarty_php_tag');
  }
  /**
   * wrapper for assign_by_ref
   *
   * @param string $tpl_var the template variable name
   * @param mixed &$value the referenced value to assign
   */
  public function assign_by_ref($tpl_var, &$value)
  {
    $this->assignByRef($tpl_var, $value);
  }
  /**
   * wrapper for append_by_ref
   *
   * @param string $tpl_var the template variable name
   * @param mixed  &$value the referenced value to append
   * @param boolean $merge  flag if array elements shall be merged
   */
  public function append_by_ref($tpl_var, &$value, $merge = false)
  {
    $this->appendByRef($tpl_var, $value, $merge);
  }
  /**
   * clear the given assigned template variable.
   *
   * @param string $tpl_var the template variable to clear
   */
  public function clear_assign($tpl_var)
  {
    $this->clearAssign($tpl_var);
  }
  /**
   * Registers custom function to be used in templates
   *
   * @param string $function   the name of the template function
   * @param string $function_impl the name of the PHP function to register
   * @param bool  $cacheable
   * @param mixed $cache_attrs
   */
  public function register_function($function, $function_impl, $cacheable = true, $cache_attrs = null)
  {
    $this->registerPlugin('function', $function, $function_impl, $cacheable, $cache_attrs);
  }
  /**
   * Unregisters custom function
   *
   * @param string $function name of template function
   */
  public function unregister_function($function)
  {
    $this->unregisterPlugin('function', $function);
  }
  /**
   * Registers object to be used in templates
   *
   * @param string $object   name of template object
   * @param object $object_impl the referenced PHP object to register
   * @param array  $allowed   list of allowed methods (empty = all)
   * @param boolean $smarty_args smarty argument format, else traditional
   * @param array  $block_methods list of methods that are block format
   *
   * @throws SmartyException
   * @internal param array $block_functs list of methods that are block format
   */
  public function register_object($object, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
  {
    settype($allowed, 'array');
    settype($smarty_args, 'boolean');
    $this->registerObject($object, $object_impl, $allowed, $smarty_args, $block_methods);
  }
  /**
   * Unregisters object
   *
   * @param string $object name of template object
   */
  public function unregister_object($object)
  {
    $this->unregisterObject($object);
  }
  /**
   * Registers block function to be used in templates
   *
   * @param string $block   name of template block
   * @param string $block_impl PHP function to register
   * @param bool  $cacheable
   * @param mixed $cache_attrs
   */
  public function register_block($block, $block_impl, $cacheable = true, $cache_attrs = null)
  {
    $this->registerPlugin('block', $block, $block_impl, $cacheable, $cache_attrs);
  }
  /**
   * Unregisters block function
   *
   * @param string $block name of template function
   */
  public function unregister_block($block)
  {
    $this->unregisterPlugin('block', $block);
  }
  /**
   * Registers compiler function
   *
   * @param string $function   name of template function
   * @param string $function_impl name of PHP function to register
   * @param bool  $cacheable
   */
  public function register_compiler_function($function, $function_impl, $cacheable = true)
  {
    $this->registerPlugin('compiler', $function, $function_impl, $cacheable);
  }
  /**
   * Unregisters compiler function
   *
   * @param string $function name of template function
   */
  public function unregister_compiler_function($function)
  {
    $this->unregisterPlugin('compiler', $function);
  }
  /**
   * Registers modifier to be used in templates
   *
   * @param string $modifier   name of template modifier
   * @param string $modifier_impl name of PHP function to register
   */
  public function register_modifier($modifier, $modifier_impl)
  {
    $this->registerPlugin('modifier', $modifier, $modifier_impl);
  }
  /**
   * Unregisters modifier
   *
   * @param string $modifier name of template modifier
   */
  public function unregister_modifier($modifier)
  {
    $this->unregisterPlugin('modifier', $modifier);
  }
  /**
   * Registers a resource to fetch a template
   *
   * @param string $type   name of resource
   * @param array $functions array of functions to handle resource
   */
  public function register_resource($type, $functions)
  {
    $this->registerResource($type, $functions);
  }
  /**
   * Unregisters a resource
   *
   * @param string $type name of resource
   */
  public function unregister_resource($type)
  {
    $this->unregisterResource($type);
  }
  /**
   * Registers a prefilter function to apply
   * to a template before compiling
   *
   * @param callable $function
   */
  public function register_prefilter($function)
  {
    $this->registerFilter('pre', $function);
  }
  /**
   * Unregisters a prefilter function
   *
   * @param callable $function
   */
  public function unregister_prefilter($function)
  {
    $this->unregisterFilter('pre', $function);
  }
  /**
   * Registers a postfilter function to apply
   * to a compiled template after compilation
   *
   * @param callable $function
   */
  public function register_postfilter($function)
  {
    $this->registerFilter('post', $function);
  }
  /**
   * Unregisters a postfilter function
   *
   * @param callable $function
   */
  public function unregister_postfilter($function)
  {
    $this->unregisterFilter('post', $function);
  }
  /**
   * Registers an output filter function to apply
   * to a template output
   *
   * @param callable $function
   */
  public function register_outputfilter($function)
  {
    $this->registerFilter('output', $function);
  }
  /**
   * Unregisters an outputfilter function
   *
   * @param callable $function
   */
  public function unregister_outputfilter($function)
  {
    $this->unregisterFilter('output', $function);
  }
  /**
   * load a filter of specified type and name
   *
   * @param string $type filter type
   * @param string $name filter name
   */
  public function load_filter($type, $name)
  {
    $this->loadFilter($type, $name);
  }
  /**
   * clear cached content for the given template and cache id
   *
   * @param string $tpl_file  name of template file
   * @param string $cache_id  name of cache_id
   * @param string $compile_id name of compile_id
   * @param string $exp_time  expiration time
   *
   * @return boolean
   */
  public function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
  {
    return $this->clearCache($tpl_file, $cache_id, $compile_id, $exp_time);
  }
  /**
   * clear the entire contents of cache (all templates)
   *
   * @param string $exp_time expire time
   *
   * @return boolean
   */
  public function clear_all_cache($exp_time = null)
  {
    return $this->clearCache(null, null, null, $exp_time);
  }
  /**
   * test to see if valid cache exists for this template
   *
   * @param string $tpl_file name of template file
   * @param string $cache_id
   * @param string $compile_id
   *
   * @return boolean
   */
  public function is_cached($tpl_file, $cache_id = null, $compile_id = null)
  {
    return $this->isCached($tpl_file, $cache_id, $compile_id);
  }
  /**
   * clear all the assigned template variables.
   */
  public function clear_all_assign()
  {
    $this->clearAllAssign();
  }
  /**
   * clears compiled version of specified template resource,
   * or all compiled template files if one is not specified.
   * This function is for advanced use only, not normally needed.
   *
   * @param string $tpl_file
   * @param string $compile_id
   * @param string $exp_time
   *
   * @return boolean results of {@link smarty_core_rm_auto()}
   */
  public function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null)
  {
    return $this->clearCompiledTemplate($tpl_file, $compile_id, $exp_time);
  }
  /**
   * Checks whether requested template exists.
   *
   * @param string $tpl_file
   *
   * @return boolean
   */
  public function template_exists($tpl_file)
  {
    return $this->templateExists($tpl_file);
  }
  /**
   * Returns an array containing template variables
   *
   * @param string $name
   *
   * @return array
   */
  public function get_template_vars($name = null)
  {
    return $this->getTemplateVars($name);
  }
  /**
   * Returns an array containing config variables
   *
   * @param string $name
   *
   * @return array
   */
  public function get_config_vars($name = null)
  {
    return $this->getConfigVars($name);
  }
  /**
   * load configuration values
   *
   * @param string $file
   * @param string $section
   * @param string $scope
   */
  public function config_load($file, $section = null, $scope = 'global')
  {
    $this->ConfigLoad($file, $section, $scope);
  }
  /**
   * return a reference to a registered object
   *
   * @param string $name
   *
   * @return object
   */
  public function get_registered_object($name)
  {
    return $this->getRegisteredObject($name);
  }
  /**
   * clear configuration values
   *
   * @param string $var
   */
  public function clear_config($var = null)
  {
    $this->clearConfig($var);
  }
  /**
   * trigger Smarty error
   *
   * @param string $error_msg
   * @param integer $error_type
   */
  public function trigger_error($error_msg, $error_type = E_USER_WARNING)
  {
    trigger_error("Smarty error: $error_msg", $error_type);
  }
}
/**
 * Smarty {php}{/php} block function
 *
 * @param array  $params  parameter list
 * @param string $content contents of the block
 * @param object $template template object
 * @param boolean &$repeat repeat flag
 *
 * @return string content re-formatted
 */
function smarty_php_tag($params, $content, $template, &$repeat)
{
  eval($content);
  return '';
}

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

PHP 相关文章推荐
通过PHP CLI实现简单的数据库实时监控调度
Jul 01 PHP
PHP中删除变量时unset()和null的区别分析
Jan 27 PHP
PHP中break及continue两个流程控制指令区别分析
Apr 18 PHP
基于PHP magic_quotes_gpc的使用方法详解
Jun 24 PHP
解析PHP中的unset究竟会不会释放内存
Jul 18 PHP
php+mysql实现数据库随机重排实例
Oct 17 PHP
PHP多线程类及用法实例
Dec 03 PHP
php正则匹配文章中的远程图片地址并下载图片至本地
Sep 29 PHP
对于Laravel 5.5核心架构的深入理解
Feb 22 PHP
Thinkphp5框架实现图片、音频和视频文件的上传功能详解
Aug 27 PHP
基于laravel Request的所有方法详解
Sep 29 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
Nov 25 PHP
php封装的smarty类完整实例
Oct 19 #PHP
PHP内存缓存功能memcached示例
Oct 19 #PHP
PHP实现上传图片到 zimg 服务器
Oct 19 #PHP
php通过会话控制实现身份验证实例
Oct 18 #PHP
利用php_imagick实现复古效果的方法
Oct 18 #PHP
php封装的page分页类完整实例
Oct 18 #PHP
php封装的单文件(图片)上传类完整实例
Oct 18 #PHP
You might like
社区(php&amp;&amp;mysql)三
2006/10/09 PHP
编写PHP脚本来实现WordPress中评论分页的功能
2015/12/10 PHP
谈谈从phpinfo中能获取哪些值得注意的信息
2017/03/28 PHP
PHP常见数组排序方法小结
2018/08/20 PHP
js监控IE火狐浏览器关闭、刷新、回退、前进事件
2014/07/23 Javascript
JavaScript生成SQL查询表单的方法
2015/08/13 Javascript
Atitit.js的键盘按键事件捆绑and事件调度
2016/04/01 Javascript
jQuery 监控键盘一段时间没输入
2016/04/22 Javascript
jQuery实现标签子元素的添加和赋值方法
2018/02/24 jQuery
vue路由懒加载的实现方法
2018/03/12 Javascript
微信小程序wx:for和wx:for-item的用法详解
2018/04/01 Javascript
手写简单的jQuery雪花飘落效果实例
2018/04/22 jQuery
vue中倒计时组件的实例代码
2018/07/06 Javascript
js实现倒计时器自定义时间和暂停
2019/02/25 Javascript
NodeJs 模仿SIP话机注册的方法
2019/06/21 NodeJs
js实现炫酷光感效果
2020/09/05 Javascript
Vue实现摇一摇功能(兼容ios13.3以上)
2021/01/26 Vue.js
JavaScript 获取滚动条位置并将页面滑动到锚点
2021/02/08 Javascript
基于Python实现的扫雷游戏实例代码
2014/08/01 Python
Python判断文件和字符串编码类型的实例
2017/12/21 Python
python破解zip加密文件的方法
2018/05/31 Python
python 利用文件锁单例执行脚本的方法
2019/02/19 Python
python实现两张图片的像素融合
2019/02/23 Python
Python在Matplotlib图中显示中文字体的操作方法
2019/07/29 Python
python脚本实现音频m4a格式转成MP3格式的实例代码
2019/10/09 Python
python爬虫使用正则爬取网站的实现
2020/08/03 Python
使用css创建三角形 使用CSS3创建3d四面体原理及代码(html5实践)
2013/01/06 HTML / CSS
美国瑜伽服装和装备购物网站:Mukha Yoga
2019/02/22 全球购物
戛纳奢侈品商店:Jacques Loup法国
2019/11/04 全球购物
Watch Station官方网站:世界一流的手表和智能手表
2020/01/05 全球购物
电力培训心得体会
2014/09/02 职场文书
党员对照检查材料
2014/09/22 职场文书
新学期红领巾广播稿
2014/10/04 职场文书
IDEA使用SpringAssistant插件创建SpringCloud项目
2021/06/23 Java/Android
PO模式在selenium自动化测试框架的优势
2022/03/20 Python
vue如何在data中引入图片的正确路径
2022/06/05 Vue.js