class.rFastTemplate.php(二)

  //
   // Description
   //    Recursive internal parse routine.  This will recursively parse a
   //    template containing dynamic inferior templates.  Each of these
   //    inferior templates gets their own entry in the TEMPLATE array.
   //
   function &parse_internal_1 ($tag, $rest = '') {
      $debug = $this->DEBUGALL || $this->DEBUG['parse_internal_1'];
      if (empty($tag)) {
         $this->error ("parse_internal_1: empty tag invalid", true);
      }
      if ($debug)
         $this->logwrite ("parse_internal_1 (tag=$tag, rest=$rest)");
      while (!empty($rest)) {
         if ($debug)
            $this->logwrite ('parse_internal_1: REGEX_DYNBEG search: rest => ' . $rest);
         if (preg_match ($this->REGEX_DYNBEG, $rest, $dynbeg)) {
            // Found match, now split into two pieces and search the second
            // half for the matching END.  The string which goes into the
            // next element includes the HTML comment which forms the BEGIN
            // block.
            if ($debug)
               $this->logwrite ('parse_internal_1: match beg => ' . $dynbeg[1]);
            $pos = strpos ($rest, $dynbeg[1]);

            // See if the text on either side of the BEGIN comment is only
            // whitespace.  If so, we delete the entire line.
            $okay = false;
            for ($offbeg = $pos - 1; $offbeg >= 0; $offbeg--) {
               $c = $rest{$offbeg};
               if ($c == "\n") {
                  $okay = true;
                  $offbeg++;
                  break;
               }
               if (($c != ' ') && ($c != "\t")) {
                  $offbeg = $pos;
                  break;
               }
            }
            if (! $okay) {
               $offend = $pos + strlen($dynbeg[1]);
            } else {
               $l = strlen ($rest);
               for ($offend = $pos + strlen($dynbeg[1]); $offend < $l; $offend++) {
                  $c = $rest{$offend};
                  if ($c == "\n") {
                     $offend++;
                     break;
                  }
                  if (($c != ' ') && ($c != "\t")) {
                     $offend = $pos + strlen($dynbeg[1]);
                     break;
                  }
               }
            }

            // This includes the contents of the REGEX_DYNBEG in the output
            // $part[] = substr ($rest, 0, $pos);
            // This preserves whitespace on the END block line(s).
            // $part[] = substr ($rest, 0, $pos+strlen($dynbeg[1]));
            // $rest = substr ($rest, $pos+strlen($dynbeg[1]));
            // Catch case where BEGIN block is at position 0.
            if ($offbeg > 0)
               $part[] = substr ($rest, 0, $offbeg);
            $rest = substr ($rest, $offend);
            $sub = '';
            if ($debug)
               $this->logwrite ("parse_internal_1: found at pos = $pos");
            // Okay, here we are actually NOT interested in just the next
            // END block.  We are only interested in the next END block that
            // matches this BEGIN block.  This is not the most efficient
            // because we really could do this in one pass through the
            // string just marking BEGIN and END blocks.  But the recursion
            // makes for a simple algorithm (if there was a reverse
            // preg...).
            $found  = false;
            while (preg_match ($this->REGEX_DYNEND, $rest, $dynend)) {
               if ($debug)
                  $this->logwrite ('parse_internal_1: REGEX_DYNEND search: rest => ' . $rest);
               if ($debug)
                  $this->logwrite ('parse_internal_1: match beg => ' . $dynend[1]);
               $pos  = strpos ($rest, $dynend[1]);
               if ($dynbeg[2] == $dynend[2]) {
                  $found  = true;
                  // See if the text on either side of the END comment is
                  // only whitespace.  If so, we delete the entire line.
                  $okay = false;
                  for ($offbeg = $pos - 1; $offbeg >= 0; $offbeg--) {
                     $c = $rest{$offbeg};
                     if ($c == "\n") {
                        $offbeg++;
                        $okay = true;
                        break;
                     }
                     if (($c != ' ') && ($c != "\t")) {
                        $offbeg = $pos;
                        break;
                     }
                  }
                  if (! $okay) {
                     $offend = $pos + strlen($dynend[1]);
                  } else {
                     $l = strlen ($rest);
                     for ($offend = $pos + strlen($dynend[1]); $offend < $l; $offend++) {
                        $c = $rest{$offend};
                        if ($c == "\n") {
                           $offend++;
                           break;
                        }
                        if (($c != ' ') && ($c != "\t")) {
                           $offend = $pos + strlen($dynend[1]);
                           break;
                        }
                     }
                  }
                  // if ($debug)
                  // $this->logwrite ("parse_internal_1: DYNAMIC BEGIN: (pos,len,beg,end) => ($pos, " . strlen($dynbeg[1]) . ", $offbeg, $offend)
                  // This includes the contents of the REGEX_DYNEND in the output
                  // $rest = substr ($rest, $pos);
                  // This preserves whitespace on the END block line(s).
                  // $rest = substr ($rest, $pos+strlen($dynend[1]));
                  // $sub .= substr ($rest, 0, $pos);
                  $sub .= substr ($rest, 0, $offbeg);
                  $rest = substr ($rest, $offend);
                  // Already loaded templates will not be reloaded.  The
                  // 'clear' test was actually hiding a bug in the clear()
                  // logic....
                  if (false && isset($this->TEMPLATE[$dynend[2]]['clear'])
                      && $this->TEMPLATE[$dynend[2]]['clear']) {
                     $this->TEMPLATE[$dynend[2]]['string']  = '';
                     $this->TEMPLATE[$dynend[2]]['result'] = '';
                     $this->TEMPLATE[$dynend[2]]['part']    =
                        $this->parse_internal_1 ($dynend[2], ' ');
                  } else if (!isset($this->TEMPLATE[$dynend[2]]['loaded'])
                             || !$this->TEMPLATE[$dynend[2]]['loaded']) {
                     // Omit pathological case of empty dynamic template.
                     if (strlen($sub) > 0) {
                        $this->TEMPLATE[$dynend[2]]['string'] = $sub;
                        $this->TEMPLATE[$dynend[2]]['part']   =
                           $this->parse_internal_1 ($dynend[2], $sub);
                        $this->TEMPLATE[$dynend[2]]['part']['parent'] = $tag;
                     }
                  }
                  $this->TEMPLATE[$dynend[2]]['loaded'] = true;
                  $part[] = &$this->TEMPLATE[$dynend[2]];
                  $this->TEMPLATE[$dynend[2]]['tag']    = $dynend[2];
                  break;
               } else {
                  $sub .= substr ($rest, 0, $pos+strlen($dynend[1]));
                  $rest = substr ($rest, $pos+strlen($dynend[1]));
                  if ($debug)
                     $this->logwrite ("parse_internal_1: $dynbeg[2] != $dynend[2]");
               }
            }
            if (!$found) {
               $this->error ("malformed dynamic template, missing END<BR />\n" .
                             "$dynbeg[1]<BR />\n", true);
            }
         } else {
            // Although it would appear to make sense to check that we don't
            // have a dangling END block, we will, in fact, ALWAYS appear to
            // have a dangling END block.  We stuff the BEGIN string in the
            // part before the inferior template and the END string in the
            // part after the inferior template.  So for this test to work,
            // we would need to look just past the final match.
            if (preg_match ($this->REGEX_DYNEND, $rest, $dynend)) {
               // $this->error ("malformed dynamic template, dangling END<BR />\n" .
               //            "$dynend[1]<BR />\n", 1);
            }
            $part[] = $rest;
            $rest = '';
         }
      }
      return $part;
   }

   //
   // Description
   //    Parse the template.  If $tag is actually an array, we iterate over
   //    the array elements.  If it is a simple string tag, we may still
   //    recursively parse the template if it contains dynamic templates and
   //    we are configured to automatically load those as well.
   //
   function parse_internal ($tag) {
      $debug = $this->DEBUGALL || $this->DEBUG['parse_internal'];
      $append = false;
      if ($debug)
         $this->logwrite ("parse_internal (tag=$tag)");

      // If we are handed an array of tags, iterate over all of them.  This
      // is really a holdover from the way class.FastTemplate.php3 worked;
      // I think subst() already pulls that array apart for us, so this
      // should not be necessary unless someone calls the internal member
      // function directly.
      if (gettype($tag) == 'array') {
         reset ($tag);
         foreach ($tag as $t) {
            $this->parse_internal ($t);
         }
      } else {
         // Load the file if it hasn't already been loaded.  It might be
         // nice to put in some logic that reloads the file if it has
         // changed since we last loaded it, but that probably gets way too
         // complicated and only makes sense if we start keeping it floating
         // around between page loads as a persistent variable.
         if (!isset($this->TEMPLATE[$tag]['loaded'])) {
            if ($this->TEMPLATE[$tag]['dynamic']) {
               // Template was declared via define_dynamic().
               if ($this->TEMPLATE[$tag]['parent'])
                  $tag = $this->TEMPLATE[$tag]['parent'];
               else {
                  // Try to find a non-dynamic template with the same file.
                  // This would have been defined via define(array(), true)
                  reset ($this->TEMPLATE);
                  foreach (array_keys($this->TEMPLATE) as $ptag) {
                     if ($debug)
                        $this->logwrite ("parse_internal: looking for non-dynamic parent, $ptag");
                     if (!$this->TEMPLATE[$ptag]['dynamic']
                         && ($this->TEMPLATE[$ptag]['file'] == $this->TEMPLATE[$tag]['file'])) {
                        $tag = $ptag;
                        break;
                     }
                  }
               }
            }
            $this->TEMPLATE[$tag]['string'] = &$this->load($this->TEMPLATE[$tag]['file']);
            $this->TEMPLATE[$tag]['loaded'] = 1;
         }

         // If we are supposed to automatically detect dynamic templates and the dynamic
         // flag is not set, scan the template for dynamic sections.  Dynamic sections
         // markers have a very rigid syntax as HTML comments....
         if ($this->DYNAMIC) {
            $this->TEMPLATE[$tag]['tag']  = $tag;
            if (!isset($this->TEMPLATE[$tag]['parsed'])
                || !$this->TEMPLATE[$tag]['parsed']) {
               $this->TEMPLATE[$tag]['part'] = $this->parse_internal_1 ($tag, $this->TEMPLATE[$tag]['string']);
               $this->TEMPLATE[$tag]['parsed'] = true;
            }
         }
      }
   }

   //
   // Description
   //    class.FastTemplate.php3 compatible interface.
   //
   // Notes
   //    I prefer the name `subst' to `parse' since during this phase we are
   //    really doing variable substitution into the template.  However, at
   //    some point we have to load and parse the template and `subst' will
   //    do that as well...
   //
   function parse ($handle, $tag, $autoload = true) {
      return $this->subst ($handle, $tag, $autoload);
   }

// 未完待续

时间: 2024-09-15 21:14:05

class.rFastTemplate.php(二)的相关文章

ps怎么把二维码设置为透明背景?

  ps怎么把二维码设置为透明背景?微信二维码在下载之后的图片是带有一个白色背景的,我们在设计图稿时,二维码带有一个白色背景非常的不方便,我们应该怎么把白色的背景去掉呢?下面我用ps简单介绍一下去掉二维码白色背景的方法. 1.首先打开photosop,新建一个透明图层,文件>新建,新建时,背景色选择透明色. 2.在这个文档中打开我们要变为透明背景的二维码,打开之后如下图所示.(二维码为自己生成,不存在广告信息) 3.然后在右侧选择图层样板,选择图层旁边的通道. 4.通道种我们会看到有rgb 红

网页动画的十二原则

  作为前端的设计师和工程师,我们用 CSS 去做样式.定位并创建出好看的网站.我们经常用 CSS 去添加页面的运动过渡效果甚至动画,但我们经常做的东西不会超过这些. 动效是一个有助于访客和消费者理解我们设计的强有力工具.这里有些原则能最大限度地应用在我们的工作中. 迪士尼经过基础工作练习的长时间累积,在 1981 年出版的 The Illusion of Life: Disney Animation 一书中发表了动画的十二个原则 (12 Principles of Animation) .这些

微信扫描二维码登录网站代码

 用户通过扫描网页提供的二维码实现登陆信息获取,大家参考使用吧 请先下载  snoopy 类   代码如下: <?php /**  *  微信公众平台PHP-SDK  *  Wechatauth为非官方微信登陆API  *  用户通过扫描网页提供的二维码实现登陆信息获取  *  主要实现如下功能:  *  get_login_code() 获取登陆授权码, 通过授权码才能获取二维码  *  get_code_image($code='') 将上面获取的授权码转换为图片二维码  *  verify

Swift语法专题十二——方法

Swift讲解专题十二--方法 一.引言         方法只是一个术语,其实就是将函数与特定的类型结合,类.结构体.枚举都可以定义方法,方法又分为实例方法和类型方法,类型方法类似于Objective-C中的类方法.Swift和Objective-C的一大不同是,Objective-C只有在类中可以定义方法. 二.实例方法基础         实例方法的语法和函数完全一致,其和具体类型的实例所关联,实例方法在调用时由类型的实例点语法进行调用来完成一些功能模块.示例如下: class Math

关于PHP的前途(二)

3.2在Windows 95/98/NT/2000上快速安装Apache Web服务器(10秒钟) 在Windows上运行PHP,你需要一个Web服务器,你可以使用微软的IIS,也可以使用免费的Apache .因为可以通过Apache的安装文件setup.exe进行安装,可以为你节省许多时间. PHPTtriad是一个包括Apache.PHP.MySQL的软件包,可从http://www.phpgeek.com/phptriad.php 或其镜像站点http://sourceforge.net/

源代码-我在学习Android 的ZXing开源项目二维码时 有几个类 不清楚他具体的意义,功能。

问题描述 我在学习Android 的ZXing开源项目二维码时 有几个类 不清楚他具体的意义,功能. ①BitMatrix.java ②ByteMatrix.java ③MultiFormatWriter.java ④QRCodeWriter.java 这4个 我实在不懂 这功能,这里面哪个 是将输入字符串 变成那个0,1 那个的?用什么算法了...我这是Android工程. 谢谢了...

jspSmartUpload上传下载全攻略(二)

js|攻略|上传|下载 二.相关类说明篇 ㈠ File类 这个类包装了一个上传文件的所有信息.通过它,可以得到上传文件的文件名.文件大小.扩展名.文件数据等信息. File类主要提供以下方法: 1.saveAs作用:将文件换名另存. 原型: public void saveAs(java.lang.String destFilePathName) 或 public void saveAs(java.lang.String destFilePathName, int optionSaveAs) 其

VPC最佳实践(二):VPC内如何使用云产品

大家知道,专有网络VPC(Virtual Private Cloud)是隔离的私有网络,默认情况下,VPC之间是无法通过私网通信的,VPC内的ECS也无法访问公网或者被公网访问,VPC更不能通过私网访问经典网络.那么就有一个问题,VPC内应该如何使用云产品呢?   云产品一般会提供如下几种网络访问能力   一是公网访问能力 如SLB提供了公网实例,VPC ECS可以通过绑定EIP提供公网访问能力,RDS 经典网络提供了外外访问域名等.   二是私网访问能力 私网访问相比公网访问更安全,更稳定,适

【机器学习PAI实践十二】机器学习实现男女声音识别分类(含语音特征提取数据和代码)

背景 随着人工智能的算法发展,对于非结构化数据的处理能力越来越受到重视,这里面的关键一环就是语音数据的处理.目前,许多关于语音识别的应用案例已经影响着我们的生活,例如一些智能音箱中利用语音发送指令,一些搜索工具利用语音输出文本代替键盘录入. 本文我们将针对语音识别中最简单的案例"男女声音"识别,结合本地的R工具以及机器学习PAI,为大家进行介绍.通过本案例,可以将任何用户的语音数据标记出性别,并且保持高准确率.我们把整个实验流程切分为两部分,第一部分是声音信号的特征提取,通过R的信号处