Home >php教程 >php手册 >网友支招:生成smarttemplate模板处理程序框架

网友支招:生成smarttemplate模板处理程序框架

WBOY
WBOYOriginal
2016-06-21 08:55:381036browse

/**
* 生成smarttemplate模板处理程序框架
*
* smarttemplate 模板类具有极高的工作效率,代码尺寸很小。
* 他的可扩展模板函数更是有独到之处。
*
* 尽管只有不多的几个方法,使用起来也很简单。
* 但是在多此调整模板文件以后,要使处理程序与模板对应起来可是要花些工夫了。

* 本程序用于分析模板文件、并给出模板处理程序的框架
**/

ob_start();
$template_name = "include/main.html";
$str = file_get_contents($template_name);
preg_match_all("/{\w.*}/u",$str,$regs);


$stack = array();
$vars = array();
$functions = array();
$key = 0; // = chr(ord("i")+count($stack));
$lf = "\r\n";
$outstr = "";
foreach($regs[0] as $v)
{
  if(preg_match_all("//",$v, $r))
  {
    array_push($stack,$r[1][0]);
    $k = chr(ord("i")+$key++);
    $outstr .= str_repeat(chr(9), $key-1)."/* 对数组 \${$r[1][0]} 赋值 */$lf";
    $outstr .= str_repeat(chr(9), $key-1)."for(\$k=0;\$k  }

  elseif(preg_match_all("//",$v, $r))
  {
    $v = array_pop($stack);
    $outstr .= str_repeat(chr(9), --$key)."
  }

  $lf";
  if($key)
  {
    $k = chr(ord("i")+$key-1);
    $p = $stack[$key-1];
    $outstr .= str_repeat(chr(9), $key)."\${$p}[\$k][$v] = \$v;$lf";
  }

  else
  $outstr .= "\$data[$v] = \$v;$lf";
}

elseif(preg_match_all("/{(\w+)}/",$v, $r))
{
  if(count($stack))
  {
    $v = $stack[$key-1];
    $k = chr(ord("i")+$key-1);
    $outstr .= str_repeat(chr(9), count($stack))."\${$v}[\$k][{$r[1][0]}] = {$r[1][0]}测试;$lf";
    $ch = "\$data[".join("][][",$stack)."][{$r[1][0]}]";
    if(! in_array($ch, $vars))
    $vars[] = $ch; 
  }

  else
  if(! in_array("\$data[{$r[1][0]}]", $vars))
  { 
    $vars[] = "\$data[{$r[1][0]}]";
    $outstr .= "\$data[{$r[1][0]}] = {$r[1][0]}测试;$lf"; 
   }
}

else

if(preg_match_all("/{(\w+):([^}]+)/",$v, $r))
{ 
  $ch = $r[1][0]."({$r[2][0]})";
  $define = true;
  if(! @include("smarttemplate_extensions/smarttemplate_extension_serverr.php"))
  $define = false;
  $functions[] = array(name=>$ch, define=>$define);
  foreach(split(",",$r[2][0]) as $v)
  if($v[0] != ")
  if(! in_array($v, $vars))
  {
    $vars[] = "\$data[$v]";
    $outstr .= "\$data[$v] = $v测试;$lf";
   }
}

else
if(preg_match_all("/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP框架之PradoNext article:PHP框架之FleaPHP