仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1222|回复: 19
打印 上一主题 下一主题

[学习教程] PHP教程之class.rFastTemplate.php(二)

[复制链接]
谁可相欹 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:23:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
不过语法好学,但是怎么用语法来实现每个人都有每个人的方式,几乎是各有千秋。然而借鉴别人成功的代码,绝对是有益无害,因此,多看那些经过千锤百炼凝出来的经典代码,是进阶的最好方法。     //
   // 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);
   }

// 未完待续  开发相册系统过程中就有过这样的问题,因为没有交流好,出现重复工作问题,因为文档没有详细的说明而经常临时问对方。
兰色精灵 该用户已被删除
沙发
发表于 2015-2-6 00:44:26 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
山那边是海 该用户已被删除
板凳
发表于 2015-2-10 12:00:11 | 只看该作者
当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标,
变相怪杰 该用户已被删除
地板
发表于 2015-2-22 05:45:44 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
飘灵儿 该用户已被删除
5#
发表于 2015-2-27 06:15:34 | 只看该作者
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
活着的死人 该用户已被删除
6#
发表于 2015-2-28 09:51:13 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
灵魂腐蚀 该用户已被删除
7#
发表于 2015-3-9 22:05:12 | 只看该作者
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
再现理想 该用户已被删除
8#
发表于 2015-3-11 08:52:01 | 只看该作者
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
老尸 该用户已被删除
9#
发表于 2015-3-11 14:46:32 | 只看该作者
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
深爱那片海 该用户已被删除
10#
发表于 2015-3-13 04:37:38 | 只看该作者
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
海妖 该用户已被删除
11#
发表于 2015-3-13 11:09:56 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
若天明 该用户已被删除
12#
发表于 2015-3-20 12:50:27 | 只看该作者
实践是检验自己会不会的真理。
因胸联盟 该用户已被删除
13#
发表于 2015-3-28 03:51:31 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
莫相离 该用户已被删除
14#
发表于 2015-4-18 02:15:15 | 只看该作者
实践是检验自己会不会的真理。
金色的骷髅 该用户已被删除
15#
发表于 2015-4-26 19:10:52 | 只看该作者
首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。
精灵巫婆 该用户已被删除
16#
发表于 2015-4-27 21:22:55 | 只看该作者
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。
蒙在股里 该用户已被删除
17#
发表于 2015-5-1 03:10:16 | 只看该作者
学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
小妖女 该用户已被删除
18#
发表于 2015-5-3 20:39:08 | 只看该作者
其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。
透明 该用户已被删除
19#
发表于 2015-6-15 06:56:04 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
爱飞 该用户已被删除
20#
发表于 2015-6-21 18:37:03 | 只看该作者
学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-4-29 14:59

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表