动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 class.rFastTemplate.php

作者来源: 
阅读 数 482 人次 , 2006-3-29 4:08:00 


 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);
 }
 //
 // 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 ("<b>unmatched tags still present in $tag</b><br />");
}
 }
} 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} <br /> \n";
if ($die) {
 exit;
}
return;
 }
}

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:检查ISBN号码是否合法
· 下一篇:class.rFastTemplate.php
· php中分页显示文章标题
· 关于PHP中操作MySQL数据库的一些要注意的问题
· apache服务器专用函数库
· 如何在PHP中判断某个函数是否被支持
· 浅谈php用户身份认证


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077