【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-class.smarttemplateparser.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?php
/**
* SmartTemplateParser Class
* Used by SmartTemplate Class
*
* @author Philipp v. Criegern philipp@criegern.com
* @version 1.01 08.05.2002
* @desc Used by SmartTemplate Class
*/
class SmartTemplateParser
{
/**
* The template itself
*
* @access private
*/
var $template;
/**
* List of used SmartTemplate Extensions
*
* @access private
*/
var $extension_tagged = array();
/**
* Error messages
*
* @access public
*/
var $error;
/**
* SmartTemplateParser Constructor
*
* @param string $template_filename HTML Template Filename
*/
function SmartTemplateParser ( $template_filename )
{
// Load Template
if ($hd = @fopen($template_filename, "r"))
{
$this->template = fread($hd, filesize($template_filename));
fclose($hd);
}
else
{
$this->template = "File not found: '$template_filename'";
}
}
/**
* Main Template Parser
*
* @param string $compiled_template_filename Compiled Template Filename
* @desc Creates Compiled PHP Template
*/
function compile( $compiled_template_filename = '' )
{
if (empty($this->template))
{
return;
}
// END, ELSE Blocks
$page = preg_replace("/<!-- ENDIF.+?-->/", "<?php\n}\n?>", $this->template);
$page = preg_replace("/<!-- END[ a-zA-Z0-9_.]* -->/", "<?php\n}\n\$_obj=\$_stack[--\$_stack_cnt];}\n?>", $page);
$page = str_replace("<!-- ELSE -->", "<?php\n} else {\n?>", $page);
// 'BEGIN - END' Blocks
if (preg_match_all('/<!-- BEGIN ([a-zA-Z0-9_.]+) -->/', $page, $var))
{
foreach ($var[1] as $tag)
{
list($parent, $block) = $this->var_name($tag);
$code = "<?php\n"
. "if (!empty(\$$parent"."['$block'])){\n"
. "if (!is_array(\$$parent"."['$block']))\n"
. "\$$parent"."['$block']=array(array('$block'=>\$$parent"."['$block']));\n"
. "\$_tmp_arr_keys=array_keys(\$$parent"."['$block']);\n"
. "if (\$_tmp_arr_keys[0]!='0')\n"
. "\$$parent"."['$block']=array(0=>\$$parent"."['$block']);\n"
. "\$_stack[\$_stack_cnt++]=\$_obj;\n"
. "foreach (\$$parent"."['$block'] as \$rowcnt=>\$$block) {\n"
. "\$$block"."['ROWCNT']=\$rowcnt;\n"
. "\$$block"."['ALTROW']=\$rowcnt%2;\n"
. "\$$block"."['ROWBIT']=\$rowcnt%2;\n"
. "\$_obj=&\$$block;\n?>";
$page = str_replace("<!-- BEGIN $tag -->", $code, $page);
}
}
// 'IF nnn="mmm"' Blocks
if (preg_match_all('/<!-- (ELSE)?IF ([a-zA-Z0-9_.]+)([!=<>]+)"([^"]*)" -->/', $page, $var))
{
foreach ($var[2] as $cnt => $tag)
{
list($parent, $block) = $this->var_name($tag);
$cmp = $var[3][$cnt];
$val = $var[4][$cnt];
$else = ($var[1][$cnt] == 'ELSE') ? '} else' : '';
if ($cmp == '=')
{
$cmp = '==';
}
$code = "<?php\n$else"."if (\$$parent"."['$block'] $cmp \"$val\"){\n?>";
$page = str_replace($var[0][$cnt], $code, $page);
}
}
// 'IF nnn' Blocks
if (preg_match_all('/<!-- (ELSE)?IF ([a-zA-Z0-9_.]+) -->/', $page, $var))
{
foreach ($var[2] as $cnt => $tag)
{
$else = ($var[1][$cnt] == 'ELSE') ? '} else' : '';
list($parent, $block) = $this->var_name($tag);
$code = "<?php\n$else"."if (!empty(\$$parent"."['$block'])){\n?>";
$page = str_replace($var[0][$cnt], $code, $page);
}
}
// Replace Scalars
if (preg_match_all('/{([a-zA-Z0-9_. >]+)}/', $page, $var))
{
foreach ($var[1] as $fulltag)
{
// Determin Command (echo / $obj[n]=)
list($cmd, $tag) = $this->cmd_name($fulltag);
list($block, $skalar) = $this->var_name($tag);
$code = "<?php\n$cmd \$$block"."['$skalar'];\n?>\n";
$page = str_replace('{'.$fulltag.'}', $code, $page);
}
}
// ROSI Special: Replace Translations
if (preg_match_all('/<"([a-zA-Z0-9_.]+)">/', $page, $var))
{
fo