0

0

class.rFastTemplate.php(三)

php中文网

php中文网

发布时间:2016-06-21 09:11:59

|

1364人浏览过

|

来源于php中文网

原创

   //
   // Description
   //    Perform substitution on the template.  We do not really recurse
   //    downward in the sense that we do not do subsitutions on inferior
   //    templates.  For each inferior template which is a part of this
   //    template, we insert the current value of their results.
   //
   // Notes
   //    Do I want to make this return a reference?
   function subst ($handle, $tag, $autoload = true) {
      $append = false;
      $debug = $this->DEBUGALL || $this->DEBUG['subst'];
      $this->LAST = $handle;

      if ($debug)
         $this->logwrite ("subst (handle=$handle, tag=$tag, autoload=$autoload)");

      // For compatibility with FastTemplate, the results need to overwrite
      // for an array.  This really only seems to be useful in the case of
      // something like
      //     $t->parse ('MAIN', array ('array', 'main'));
      // Where the 'main' template has a variable named MAIN which will be
      // set on the first pass (i.e., when parasing 'array') and used on the
      // second pass (i.e., when parsing 'main').
      if (gettype($tag) == 'array') {
         foreach (array_values($tag) as $t) {
            if ($debug)
               $this->logwrite ("subst: calling subst($handle,$t,$autoload)");
            $this->subst ($handle, $t, $autoload);
         }
         return $this->HANDLE[$handle];
      }

      // Period prefix means append result to pre-existing value.
      if (substr($tag,0,1) == '.') {
         $append = true;
         $tag = substr ($tag, 1);
         if ($debug)
            $this->logwrite ("subst (handle=$handle, tag=$tag, autoload=$autoload) in append mode");
      }
      // $this->TEMPLATE[$tag] will only be set if it was explicitly
      // declared via define(); i.e., inferior templates will not have an
      // entry.
      if (isset($this->TEMPLATE[$tag])) {
         if (!isset($this->TEMPLATE[$tag]['parsed'])
             || !$this->TEMPLATE[$tag]['parsed'])
            $this->parse_internal ($tag);
      } else {
         if (!$this->DYNAMIC) {
            $this->error ("subst (handle=$handle, tag=$tag, autoload=$autoload): " .
                          'no such tag and dynamic templates are turned off', true);
         }
         if ($autoload) {
            if ($debug)
               $this->logwrite ("subst: TEMPLATE[tag=$tag] not found, trying autoload");
            foreach (array_keys($this->TEMPLATE) as $t) {
               if ($debug)
                  $this->logwrite ("subst: calling parse_internal (tag=$t)");
               if (!isset($this->TEMPLATE[$tag]['parsed'])
                   || !$this->TEMPLATE[$tag]['parsed'])
                  $this->parse_internal ($t);
            }
            if ($debug)
               $this->logwrite ('subst: retrying with autoload = false');
            $this->subst ($handle, $tag, false);
            if ($debug)
               $this->logwrite ('subst: completed with autoload = false');
            return;
         } else {
            $this->error ("subst (handle=$handle, tag=$tag, autoload=$autoload):  no such tag", true);
         }
      }
      if (!$append) {
         $this->TEMPLATE[$tag]['result'] = '';
         if ($debug)
            $this->logwrite ("subst (handle=$handle, tag=$tag, autoload=$autoload) in overwrite mode");
      }
      if ($debug)
         $this->logwrite ('subst: type(this->TEMPLATE[$tag][\'part\']) => ' .
                          gettype($this->TEMPLATE[$tag]['part']));
      // Hmmm, clear() called before subst() seems to result in this not
      // being defined which leaves me a bit confused....
      $result = '';
      if (isset($this->TEMPLATE[$tag]['part'])) {
         reset ($this->TEMPLATE[$tag]['part']);
         foreach (array_keys($this->TEMPLATE[$tag]['part']) as $p) {
            if ($debug)
               $this->logwrite ("subst: looking at TEMPLATE[$tag]['part'][$p]");
            $tmp = $this->TEMPLATE[$tag]['part'][$p];
            // Don't try if ($p == 'parent')....
            if (strcmp ($p, 'parent') == 0) {
               if ($debug)
                  $this->logwrite ("subst: skipping part $p");
               $tmp = '';
            } else if (gettype($this->TEMPLATE[$tag]['part'][$p]) == 'string') {
               if ($debug)
                  $this->logwrite ("subst: using part $p");
               reset ($this->VAR);
               // Because we treat VAR and HANDLE separately (unlike
               // class.FastTemplate.php3), we have to iterate over both or we
               // miss some substitutions and are not 100% compatible.
               while (list($key,$val) = each ($this->VAR)) {
                  if ($debug)
                     $this->logwrite ("subst: substituting VAR $key = $val in $tag");
                  $key = '{'.$key.'}';
                  $tmp = str_replace ($key, $val, $tmp);
               }
               reset ($this->HANDLE);
               while (list($key,$val) = each ($this->HANDLE)) {
                  if ($debug)
                     $this->logwrite ("subst: substituting HANDLE $key = $val in $tag");
                  $key = '{'.$key.'}';
                  $tmp = str_replace ($key, $val, $tmp);
               }
               $result .= $tmp;
            } else {
               $xtag = $this->TEMPLATE[$tag]['part'][$p]['tag'];
               if ($debug) {
                  $this->logwrite ("subst: substituting other tag $xtag result in $tag");
               }
               // The assignment is a no-op if the result is not set, but when
               // E_ALL is in effect, a warning is generated without the
               // isset() test.
               if (isset ($this->TEMPLATE[$xtag]['result']))
                  $result .= $this->TEMPLATE[$xtag]['result'];
            }
         }
      }
      if ($this->STRICT) {
     // If quiet-mode is turned on, skip the check since we're not going
     // to do anything anyway.
     if (!$this->QUIET) {
        if (preg_match ($this->REGEX_VAR, $result)) {
           $this->error ("unmatched tags still present in $tag
");
        }
     }
      } else {
         $result = preg_replace ($this->REGEX_VAR, '', $result);
      }
      if ($append) {
         if ($debug) {
            $this->logwrite ("subst: appending TEMPLATE[$tag]['result'] = $result");
            $this->logwrite ("subst: old HANDLE[$handle] = {$this->HANDLE[$handle]}");
            $this->logwrite ("subst: old TEMPLATE[$tag]['result'] = {$this->TEMPLATE[$tag]['result']}");
         }
         // The isset() tests are to suppresss warning when E_ALL is in effect
         // and the variables have not actually been set yet (even though the
         // user specified append-mode).
         if (isset ($this->HANDLE[$handle]))
            $this->HANDLE[$handle] .= $result;
         else
            $this->HANDLE[$handle] = $result;
         if (isset ($this->TEMPLATE[$tag]['result']))
            $this->TEMPLATE[$tag]['result'] .= $result;
         else
            $this->TEMPLATE[$tag]['result'] = $result;
         if ($debug) {
            $this->logwrite ("subst: new HANDLE[$handle] = {$this->HANDLE[$handle]}");
            $this->logwrite ("subst: new TEMPLATE[$tag]['result'] = {$this->TEMPLATE[$tag]['result']}");
         }

      } else {
         if ($debug)
            $this->logwrite ("subst: setting TEMPLATE[$tag]['result'] = $result");
         $this->HANDLE[$handle]  = $result;
         $this->TEMPLATE[$tag]['result'] = $result;
      }
      return $this->HANDLE[$handle];
   }

   //
   // Description
   //    Clear a block from a template.  The intent is to remove an inferior
   //    template from a parent.  This works even if the template has already
   //    been parsed since we go straight to the specified template and clear
   //    the results element.  If the given template has not yet been
   //    loaded, the load is forced by calling parse_internal().
   //
   function clear_dynamic ($tag = NULL) {
      $debug = $this->DEBUGALL || $this->DEBUG['clear_dynamic'];
      if (is_null ($tag)) {
         // Clear all result elements.  Uhm, needs to be tested.
         if ($debug)
            $this->logwrite ("clear_dynamic (NULL)");
         foreach (array_values ($this->TEMPLATE) as $t) {
            $this->clear_dynamic ($t);
         }
         return;
      } else if (gettype($tag) == 'array') {
         if ($debug)
            $this->logwrite ("clear_dynamic ($tag)");
         foreach (array_values($tag) as $t) {
            $this->clear_dynamic ($t);
         }
         return;
      }
      else if (!isset($this->TEMPLATE[$tag])) {
         if ($debug)
            $this->logwrite ("clear_dynamic ($tag) --> $tag not set, calling parse_internal");
         $this->parse_internal ($tag);
         // $this->TEMPLATE[$tag] = array ();
      }
      if ($debug)
         $this->logwrite ("clear_dynamic ($tag)");
      // $this->TEMPLATE[$tag]['loaded']  = true;
      // $this->TEMPLATE[$tag]['string']  = '';
      $this->TEMPLATE[$tag]['result'] = '';
      // $this->TEMPLATE[$tag]['clear']   = true;
   }

   //
   // Description
   //    Clear the results of a handle set by parse().  The input handle can
   //    be a single value, an array, or the PHP constant NULL.  For the
   //    last case, all handles cleared.
   //
   function clear ($handle = NULL) {
      $debug = $this->DEBUGALL || $this->DEBUG['clear'];
      if (is_null ($handle)) {
         // Don't bother unsetting them, just set the whole thing to a new,
         // empty array.
         if ($debug)
            $this->logwrite ("clear (NULL)");
         $this->HANDLE = array ();
      } else if (gettype ($handle) == 'array') {
         if ($debug)
            $this->logwrite ("clear ($handle)");
         foreach (array_values ($handle) as $h) {
            $this->clear ($h);
         }
      } else if (isset ($this->HANDLE[$handle])) {
         if ($debug)
            $this->logwrite ("clear ($handle)");
         unset ($this->HANDLE[$handle]);
      }
   }

   //
   // Description
   //   Clears all information associated with the specified tag as well as
   //   any information associated with embedded templates.  This will force
   //   the templates to be reloaded on the next call to subst().
   //   Additionally, any results of previous calls to subst() will also be
   //   cleared.
   //
   // Notes
   //   This leaves dangling references in $this->HANDLE.  Or does PHP do
   //   reference counting so they are still valid?
   //
   function unload ($tag) {
      if (!isset($this->TEMPLATE[$tag]))
         return;
      if (isset ($this->TEMPLATE[$tag]['parent'])) {
         $ptag = $this->TEMPLATE[$tag]['parent'];
         foreach (array_keys($this->TEMPLATE) as $t) {
            if ($this->TEMPLATE[$t]['parent'] == $ptag) {
               unset ($this->TEMPLATE[$t]);
            }
         }
      }
      unset ($this->TEMPLATE[$tag]);
      return;
   }

   //
   // Description
   //    class.FastTemplate.php3 compatible interface.
   //
   function assign ($tplkey, $rest = '') {
      $this->setkey ($tplkey, $rest);
   }

   //
   // Description
   //    Set a (key,value) in our internal variable array.  These will be
   //    used during the substitution phase to replace template variables.
   //
   function setkey ($tplkey, $rest = '') {
      if (gettype ($tplkey) == 'array') {
         reset ($tplkey);
         while (list($key,$val) = each ($tplkey)) {
            if (!empty($key)) {
               $this->VAR[$key] = $val;
            }
         }
      } else {
         if (!empty($tplkey)) {
            $this->VAR[$tplkey] = $rest;
         }
      }
   }

   //
   // Description
   //    class.FastTemplate.php3 compatible interface
   //
   function get_assigned ($key = '') {
      return $this->getkey ($key);
   }

   //
   // Description
   //    Retrieve a value from our internal variable array given the key name.
   //
   function getkey ($key = '') {
      if (empty($key)) {
         return false;
      } else if (isset ($this->VAR[$key])) {
         return $this->VAR[$key];
      } else {
         return false;
      }
   }

   function fetch ($handle = '') {
      if (empty($handle)) {
         $handle = $this->LAST;
      }
      return $this->HANDLE[$handle];
   }

   function xprint ($handle = '') {
      if (empty($handle)) {
         $handle = $this->LAST;
      }
      print ($this->HANDLE[$handle]);
   }

   function FastPrint ($handle = '') {
      $this->xprint ($handle);
   }

   function clear_href ($key = '') {
      $this->unsetkey ($key);
   }

   function unsetkey ($key = '') {
      if (empty($key)) {
         unset ($this->VAR);
         $this->VAR = array ();
      } else if (gettype($key) == 'array') {
         reset ($key);
         foreach (array_values($key) as $k) {
            unset ($this->VAR[$k]);
         }
      } else {
         unset ($this->VAR[$key]);
      }
   }

   function define_nofile ($stringList, $dynamic = 0) {
      $this->define_raw ($stringList, $dynamic);
   }
   //
   // Description
   //    Member function to control explicit error messages.  We don't do
   //    real PHP error handling.
   //
   function error ($errorMsg, $die = 0) {
      $this->ERROR = $errorMsg;
      echo "ERROR: {$this->ERROR}
\n";
      if ($die) {
         exit;
      }
      return;
   }
}

//全部结束



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

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

下载

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Python 序列化
Python 序列化

本专题整合了python序列化、反序列化相关内容,阅读专题下面的文章了解更多详细内容。

0

2026.02.02

AO3官网入口与中文阅读设置 AO3网页版使用与访问
AO3官网入口与中文阅读设置 AO3网页版使用与访问

本专题围绕 Archive of Our Own(AO3)官网入口展开,系统整理 AO3 最新可用官网地址、网页版访问方式、正确打开链接的方法,并详细讲解 AO3 中文界面设置、阅读语言切换及基础使用流程,帮助用户稳定访问 AO3 官网,高效完成中文阅读与作品浏览。

91

2026.02.02

主流快递单号查询入口 实时物流进度一站式追踪专题
主流快递单号查询入口 实时物流进度一站式追踪专题

本专题聚合极兔快递、京东快递、中通快递、圆通快递、韵达快递等主流物流平台的单号查询与运单追踪内容,重点解决单号查询、手机号查物流、官网入口直达、包裹进度实时追踪等高频问题,帮助用户快速获取最新物流状态,提升查件效率与使用体验。

27

2026.02.02

Golang WebAssembly(WASM)开发入门
Golang WebAssembly(WASM)开发入门

本专题系统讲解 Golang 在 WebAssembly(WASM)开发中的实践方法,涵盖 WASM 基础原理、Go 编译到 WASM 的流程、与 JavaScript 的交互方式、性能与体积优化,以及典型应用场景(如前端计算、跨平台模块)。帮助开发者掌握 Go 在新一代 Web 技术栈中的应用能力。

11

2026.02.02

PHP Swoole 高性能服务开发
PHP Swoole 高性能服务开发

本专题聚焦 PHP Swoole 扩展在高性能服务端开发中的应用,系统讲解协程模型、异步IO、TCP/HTTP/WebSocket服务器、进程与任务管理、常驻内存架构设计。通过实战案例,帮助开发者掌握 使用 PHP 构建高并发、低延迟服务端应用的工程化能力。

5

2026.02.02

Java JNI 与本地代码交互实战
Java JNI 与本地代码交互实战

本专题系统讲解 Java 通过 JNI 调用 C/C++ 本地代码的核心机制,涵盖 JNI 基本原理、数据类型映射、内存管理、异常处理、性能优化策略以及典型应用场景(如高性能计算、底层库封装)。通过实战示例,帮助开发者掌握 Java 与本地代码混合开发的完整流程。

5

2026.02.02

go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

62

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

55

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

27

2026.01.31

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
JavaScript高级框架设计视频教程
JavaScript高级框架设计视频教程

共22课时 | 3.6万人学习

AngularJS教程
AngularJS教程

共24课时 | 3.3万人学习

CSS3实现按钮特效视频教程
CSS3实现按钮特效视频教程

共15课时 | 3.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号