//
// 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;
}
}
//全部结束菜鸟学堂: