class.rFastTemplate.php(一)

老婷吖_5724

老婷吖_5724

2016-06-21

934人浏览

原创

//
// Copyright ?2000-2001, Roland Roberts
//             2001 Alister Bulman Re-Port multi template-roots + more
// PHP3 Port: Copyright ?1999 CDI , All Rights Reserved.
// Perl Version: Copyright ?1998 Jason Moore , All Rights Reserved.
//
// RCS Revision
//   @(#) $Id: class.rFastTemplate.php,v 1.22 2001/10/18 21:36:53 roland Exp $
//   $Source: /home/cvs/projects/php/tools/class.rFastTemplate.php,v $
//
// Copyright Notice
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2, or (at your option)
//    any later version.
//
//    class.rFastTemplate.php 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
//    General Public License for more details.
//
// Comments
//
//    I would like to thank CDI for pointing out the
//    copyright notice attached to his PHP3 port which I had blindly missed
//    in my first release of this code.
//
//    This work is derived from class.FastTemplate.php3 version 1.1.0 as
//    available from http://www.thewebmasters.net/.  That work makes
//    reference to the "GNU General Artistic License".  In correspondence
//    with the author, the intent was to use the GNU General Public License;
//    this work does the same.
//
// Authors
//
//    Roland Roberts
//    Alister Bulman (multi template-roots)
//    Michal Rybarik (define_raw())
//    CDI , PHP3 port
//    Jason Moore , original Perl version
//
// Synopsis
//
//    require ("PATH-TO-TEMPLATE-CODE/class.Template.php");
//    $t = new Template("PATH-TO-TEMPLATE-DIRECTORY");
//    $t->define (array(MAIN => "diary.html"));
//    $t->setkey (VAR1, "some text");
//    $t->subst (INNER, "inner")
//    $t->setkey (VAR1, "some more text");
//    $t->subst (INNER, ".inner")
//    $t->setkey (VAR2, "var2 text");
//    $t->subst (CONTENT, "main");
//    $t->print (CONTENT);
//
// Description
//
//    This is a class.FastTemplate.php3 replacement that provides most of the
//    same interface but has the ability to do nested dynamic templates.  The
//    default is to do dynamic template expansion and no special action is
//    required for this to happen.
//
// class.FastTemplate.php3 Methods Not Implemented
//
//    clear_parse
//       Same as clear.  In fact, it was the same as clear in FastTemplate.
//    clear_all
//       If you really think you need this, try
//          unset $t;
//          $t = new Template ($path);
//       which gives the same effect.
//    clear_tpl
//       Use unload instead.  This has the side effect of unloading all parent
//       and sibling templates which may be more drastic than you expect and
//       is different from class.FastTemplate.php3.  This difference is
//       necessary since the only way we can force the reload of an embedded
//       template is to force the reload of the parent and sibling templates.
//
// class.FastTemplate.php3 Methods by Another Name
//
//    The existence of these functions is a historical artifact.  I
//    originally had in mind to write a functional equivalent from scratch.
//    Then I came my senses and just grabbed class.FastTemplate.php3 and
//    started hacking it.  So, you can use the names on the right, but the
//    ones on the left are equivalent and are the names used in the original
//    class.FastTemplate.php3.
//
//      parse        --> subst
//      get_assiged  --> getkey
//      assign       --> setkey
//      clear_href   --> unsetkey
//      clear_assign --> unsetkey
//      FastPrint    --> xprint
//

class rFastTemplate {

   // File name to be used for debugging output.  Needs to be set prior to
   // calling anything other than option setting commands (debug, debugall,
   // strict, dynamic) because once the file has been opened, this is ignored.
   var $DEBUGFILE = '/tmp/class.rFastTemplate.php.dbg';

   // File descriptor for debugging output.
   var $DEBUGFD = -1;

   // Array for individual member functions.  You can turn on debugging for a
   // particular member function by calling $this->debug(FUNCTION_NAME)
   var $DEBUG = array ();

   // Turn this on to turn on debugging in all member functions via
   // $this->debugall().  Turn if off via $this->debugall(false);
   var $DEBUGALL = false;

   // Names of actual templates.  Each element will be an array with template
   // information including is originating file, file load status, parent
   // template, variable list, and actual template contents.
   var $TEMPLATE = array();

   //  Holds paths-to-templates (See: set_root and FindTemplate)
   var $ROOT     = array();

   //  Holds the HANDLE to the last template parsed by parse()
   var $LAST     = '';


   // Strict template checking.  Unresolved variables in templates will generate a
   // warning.
   var $STRICT   = true;

   // If true, this suppresses the warning generated by $STRICT=true.
   var $QUIET    = false;

   // Holds handles assigned by a call to parse().
   var $HANDLE   = array();

   // Holds all assigned variable names and values.
   var $VAR      = array();

   // Set to true is this is a WIN32 server.  This was part of the
   // class.FastTemplate.php3 implementation and the only real place it kicks
   // in is in setting the terminating character on the value of $ROOT, the
   // path where all the templates live.
   var $WIN32    = false;

   // Automatically scan template for dynamic templates and assign new values
   // to TEMPLATE based on whatever names the HTML comments use.  This can be
   // changed up until the time the first parse() is called.  Well, you can
   // change it anytime, but it will have no effect on already loaded
   // templates.  Also, if you have dynamic templates, the first call to parse
   // will load ALL of your templates, so changing it after that point will
   // have no effect on any defined templates.
   var $DYNAMIC   = true;

   // Grrr.  Don't try to break these extra long regular expressions into
   // multiple lines for readability.  PHP 4.03pl1 chokes on them if you do.
   // I'm guessing the reason is something obscure with the parenthesis
   // matching, the same sort of thing Tcl might have, but I'm not sure.

   // Regular expression which matches the beginning of a dynamic/inferior
   // template.  The critical bit is that we need two parts: (1) the entire
   // match, and (2) the name of the dynamic template.  The first part is
   // required because will do a strstr() to split the buffer into two
   // pieces: everything before the dynamic template declaration and
   // everything after.  The second is needed because after finding a BEGIN
   // we will search for an END and they both have to have the same name of
   // we consider the template malformed and throw and error.

   // Both of these are written with PCRE (Perl-Compatible Regular
   // Expressions) because we need the non-greedy operators to insure that
   // we don't read past the end of the HTML comment marker in the case that
   // the BEGIN/END block have trailing comments after the tag name.
   var $REGEX_DYNBEG = '/()/';

   // Regular expression which matches the end of a dynamic/inferior
   // template; see the comment about on the BEGIN match.
   var $REGEX_DYNEND = '/()/';
   // Regular expression which matches a variable in the template.

   var $REGEX_VAR = '/{[A-Za-z][-_A-Za-z0-9]*}/';
   //
   // Description
   //    Constructor.
   //
   function rFastTemplate ($pathToTemplates = '') {

      // $pathToTemplates can also be an array of template roots, handled in set_root
      global $php_errormsg;
      if (!empty($pathToTemplates)) {
         $this->set_root ($pathToTemplates);
      }
      $this->DEBUG = array ('subst' => false,
                            'parse_internal' => false,
                            'parse_internal_1' => false,
                            'parsed' => false,
                            'clear' => false,
                            'clear_dynamic' => false,
                            'load' => false);

      return $this;
   }

   //
   // Description
   //    Set the name to be used for debugging output.  If another file has
   //    already been opened, close it so the next call to logwrite will
   //    reopen under this name.
   //
   function debugfile ($name) {
      $this->DEBUGFILE = $name;
   }

   //
   // Description
   //    Turn on/off debugging output of an individual member function.
   //
   function debug ($what, $on = true) {
      $this->DEBUG[$what] = $on;
   }

   //
   // Description
   //    Turn on/off debugging output of all member functions.
   //
   function debugall ($on = true) {
      $this->DEBUGALL = $on;
   }

   //
   // Description
   //    Turn on/off automatic dynamic template expansion.  Note that a
   //    template with an inferior dynamic template embedded will still
   //    parse but only as if it were part of the main template.  When this
   //    is turned on, it will be parsed out as as if it were a full-blown
   //    template and can thus be both parsed and appended to as a separate
   //    entity.
   //
   function dynamic ($on = true) {
      $this->DYNAMIC = $on;
   }

   //
   // Description
   //    Turn on/off strict template checking.  When on, all template tags
   //    must be assigned or we throw an error (but stilll parse the
   //    template).
   //
   function strict ($on = true) {
      $this->STRICT = $on;
   }

   function quiet ($on = true) {
      $this->QUIET = true;
   }

   //
   // Description
   //    For compatibility with class.FastTemplate.php3.
   //
   function no_strict () {
      $this->STRICT = false;
   }

   //
   // Description
   //    Utility function for debugging.
   //
   function logwrite ($msg) {
      if ($this->DEBUGFD          $this->DEBUGFD = fopen ($this->DEBUGFILE, 'a');
      }
      fputs ($this->DEBUGFD,
             strftime ('%Y/%m/%d %H:%M:%S ') . $msg . " ");
   }

   //
   // Description
   //    This was lifted as-is from class.FastTemplate.php3.  Based on what
   //    platform is in use, it makes sure the path specification ends with
   //    the proper path separator; i.e., a slash on unix systems and a
   //    back-slash on WIN32 systems.  When we can run on Mac or VMS I guess
   //    we'll worry about other characters....
   //
   //    $root can now be an array of template roots which will be searched to
   //    find the first matching name.
   function set_root ($root) {

      if (!is_array($root)) {
         $trailer = substr ($root, -1);
         if ($trailer != ($this->WIN32 ? '\' : '/'))
            $root .= ($this->WIN32 ? '\' : '/');

         if (!is_dir($root)) {
            $this->error ("Specified ROOT dir [$root] is not a directory", true);
            return false;
         }
         $this->ROOT[] = $root;
      } else {
         reset($root);
         while(list($k, $v) = each($root)) {
            if (is_dir($v)) {
               $trailer = substr ($v,-1);
               if ($trailer != ($this->WIN32 ? '\' : '/'))
                  $v .= ($this->WIN32 ? '\' : '/');
               $this->ROOT[] = $v;
            } else
               $this->error ("Specified ROOT dir [$v] is not a directory", true);
         }
      }
   }

   //
   // Description
   //    Associate files with a template names.
   //
   // Sigh.  At least with the CVS version of PHP, $dynamic = false sets it
   // to true.
   //
   function define ($fileList, $dynamic = 0) {
      reset ($fileList);
      while (list ($tpl, $file) = each ($fileList)) {
         $this->TEMPLATE[$tpl] = array ('file' => $file, 'dynamic' => $dynamic);
      }
      return true;
   }

   function define_dynamic ($tplList, $parent='') {
      if (is_array($tplList)) {
         reset ($tplList);
         while (list ($tpl, $parent) = each ($tplList)) {
            $this->TEMPLATE[$tpl]['parent'] = $parent;
            $this->TEMPLATE[$tpl]['dynamic'] = true;
         }
      } else {
         // $tplList is not an array, but a single child/parent pair.
         $this->TEMPLATE[$tplList]['parent'] = $parent;
         $this->TEMPLATE[$tplList]['dynamic'] = true;
      }
   }

   //
   // Description
   //    Defines a template from a string (not a file). This function has
   //    not been ported from original PERL module to CDI's
   //    class.FastTemplate.php3, and it comebacks in rFastTemplate
   //    class. You can find it useful if you want to use templates, stored
   //    in database or shared memory.
   //
   function define_raw ($stringList, $dynamic = 0) {
      reset ($stringList);
      while (list ($tpl, $string) = each ($stringList)) {
         $this->TEMPLATE[$tpl] = array ('string' => $string, 'dynamic' => $dynamic, 'loaded' => 1);
      }
      return true;
   }

   //
   // Description
   //     Try each directory in our list of possible roots in turn until we
   //     find a matching template
   //
   function FindTemplate ($file) {
      // first try for a template in the current directory short path for
      // absolute filenames
      if (substr($file, 0, 1) == '/') {
         if (file_exists($file)) {
            return $file;
         }
      }

      // search path for a matching file
      reset($this->ROOT);
      while(list($k, $v) = each($this->ROOT)) {
         $f = $v . $file;
         if (file_exists($f)) {
            return $f;
         }
      }

      $this->error ("FindTemplate: file $file does not exist anywhere in " . implode(' ', $this->ROOT), true);
      return false;
   }


   //
   // Description
   //    Load a template into memory from the underlying file.
   //
   function &load ($file) {
      $debug = $this->DEBUGALL || $this->DEBUG['load'];
      if (! count($this->ROOT)) {
         if ($debug)
            $this->logwrite ("load: cannot open template $file, template base directory not set");
         $this->error ("cannot open template $file, template base directory not set", true);
         return false;
      } else {
         $contents = '';

         $filename = $this->FindTemplate ($file);

         if ($filename)
            $contents = implode ('', (@file($filename)));
         if (!($contents) or (empty($contents)) or (! $filename)) {
            if ($debug)
               $this->logwrite ("load: failed to load $file, $php_errormsg");
            $this->error ("load($file) failure: $php_errormsg", true);
         } else {
            if ($debug)
               $this->logwrite ("load: found $filename");
            return $contents;
         }
      }
   }

// 未完待续 呵呵       ——————by 孤狼



php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

140

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

100

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

80

15

Postman身份认证怎么配置
Postman身份认证怎么配置

本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。

2026.11.06

160

12

Postman请求头Headers怎么设置
Postman请求头Headers怎么设置

围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。

2026.11.06

140

13

Postman接口请求发送教程
Postman接口请求发送教程

详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。

2026.11.06

120

10

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

2026.11.05

120

12

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

2026.11.05

100

12

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

2026.11.04

120

16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Bootstrap 入门安装配置
Bootstrap 入门安装配置

共0课时 | 0人学习

JavaScript高级框架设计视频教程
JavaScript高级框架设计视频教程

共22课时 | 4.4万人学习