我正在尝试按照名为"滚动自己的模板系统"的教程。它非常草率但我设法修复了大部分作者的错误。我猜这个错误来自调用_parse_template方法的generate_markup方法。所以它肯定来自咖喱方法。我想它并不理解Template::part
。有什么想法吗?
的index.php
<?php
require_once "build/class/Template.class.php";
$template = new Template;
$template->template_file = "header.php";
echo $template->generate_markup();
_parse_template,_curry和generate_markup
private function _parse_template($extra=NULL) {
$template = $this->_template;
$comment_pattern = array('#/\*.*?\*/#s', '#(?<!:)//.*#');
$template = preg_replace($comment_pattern, NULL, $template);
$pattern = '#.*{loop}(.*?){/loop}.*#is';
$entry_template = preg_replace($pattern, "$1", $template);
$header = trim(preg_replace('/^(.*)?{loop.*$/is', "$1", $template));
if($header===$template) {
$header = NULL;
}
$footer = trim(preg_replace('#^.*?{/loop}(.*)$#is', "$1", $template));
if($footer===$template) {
$footer = NULL;
}
$tag_pattern = '/{(\w+)}/';
$callback = $this->_curry('Template::replace_tags', 2);
$markup = NULL;
for($i=0, $c=count($this->entries); $i<$c; ++$i ) {
$markup .= preg_replace_callback($tag_pattern, $callback(serialize($this->entries[$i])), $entry_template);
}
return $markup;
}
public function _curry($func, $arity) {
return create_function('', "
\$args = func_get_args();
if(count(\$args) >= $arity) {
return call_user_func_array('$func', \$args);
}
\$args = var_export(\$args, 1);
return create_function('','
\$a = func_get_args();
\$z = ' . \$args . ';
\$a = array_merge(\$z,\$a);
return call_user_func_array('$func', \$a);
');
");
}
public function generate_markup($extra=array()) {
$this->_load_template();
return $this->_parse_template($extra);
}
错误
Parse error: syntax error, unexpected 'Template' (T_STRING) in C:\xampp2\htdocs\build\class\Template.class.php(62) : runtime-created function on line 11