PHP教程之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);
$pos = strpos ($rest, $dynbeg);
// 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);
} else {
$l = strlen ($rest);
for ($offend = $pos + strlen($dynbeg); $offend < $l; $offend++) {
$c = $rest{$offend};
if ($c == "\n") {
$offend++;
break;
}
if (($c != ' ') && ($c != "\t")) {
$offend = $pos + strlen($dynbeg);
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));
// $rest = substr ($rest, $pos+strlen($dynbeg));
// 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);
$pos= strpos ($rest, $dynend);
if ($dynbeg == $dynend) {
$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);
} else {
$l = strlen ($rest);
for ($offend = $pos + strlen($dynend); $offend < $l; $offend++) {
$c = $rest{$offend};
if ($c == "\n") {
$offend++;
break;
}
if (($c != ' ') && ($c != "\t")) {
$offend = $pos + strlen($dynend);
break;
}
}
}
// if ($debug)
// $this->logwrite ("parse_internal_1: DYNAMIC BEGIN: (pos,len,beg,end) => ($pos, " . strlen($dynbeg) . ", $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));
// $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]['clear'])
&& $this->TEMPLATE[$dynend]['clear']) {
$this->TEMPLATE[$dynend]['string']= '';
$this->TEMPLATE[$dynend]['result'] = '';
$this->TEMPLATE[$dynend]['part'] =
$this->parse_internal_1 ($dynend, ' ');
} else if (!isset($this->TEMPLATE[$dynend]['loaded'])
|| !$this->TEMPLATE[$dynend]['loaded']) {
// Omit pathological case of empty dynamic template.
if (strlen($sub) > 0) {
$this->TEMPLATE[$dynend]['string'] = $sub;
$this->TEMPLATE[$dynend]['part'] =
$this->parse_internal_1 ($dynend, $sub);
$this->TEMPLATE[$dynend]['part']['parent'] = $tag;
}
}
$this->TEMPLATE[$dynend]['loaded'] = true;
$part[] = &$this->TEMPLATE[$dynend];
$this->TEMPLATE[$dynend]['tag'] = $dynend;
break;
} else {
$sub .= substr ($rest, 0, $pos+strlen($dynend));
$rest = substr ($rest, $pos+strlen($dynend));
if ($debug)
$this->logwrite ("parse_internal_1: $dynbeg != $dynend");
}
}
if (!$found) {
$this->error ("malformed dynamic template, missing END<BR />\n" .
"$dynbeg<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<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);
}
// 未完待续开发相册系统过程中就有过这样的问题,因为没有交流好,出现重复工作问题,因为文档没有详细的说明而经常临时问对方。 我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。 当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标, 使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的 使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。 最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。 在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、 因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax 在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、 说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年具体的记不清啦,囧。 写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。 实践是检验自己会不会的真理。 小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。 实践是检验自己会不会的真理。 首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。 为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。 学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql 其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。 Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81 学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
页:
[1]