[ PHPXref.com ] [ Generated: Sun Jul 20 18:31:52 2008 ] [ Mailing List 1.03 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/lib/ -> template.php (source)

   1  <?php
   2  require_once  _LIBPATH . "common.php";
   3  require_once  _LIBPATH . "xml.php";
   4  $_TSM = array();
   5  class CTemplate {
   6  var $input;
   7  var $output;
   8  var $blocks;
   9  function CTemplate($source,$source_type = "file") {
  10  $this->Load($source,$source_type);
  11  }
  12  function Load($source,$source_type = "file") {
  13  switch ($source_type) {
  14  case "file":
  15  $data = GetFileContents($source);
  16  break;
  17  case "rsl":
  18  case "string":
  19  $data = $source;
  20  break;
  21  }
  22  preg_match_all("'<!--S\:.*?-->.*?<!--E\:.*?-->'si",$data,$matches);
  23  if (count($matches[0]) != 0)
  24  foreach ($matches[0] as $block) {
  25  $name = substr($block,strpos($block,"S:") + 2,strpos($block,"-->") - 6);
  26  $block = substr($block,9 + strlen($name),strlen($block) - 18 - strlen($name) * 2);
  27  $this->blocks["$name"] = new CTemplate($block,"string");
  28  }
  29  $this->input = $this->output = preg_replace(array("'<!--S\:.*?-->(\r\n|\n|\n\r)'si","'<!--E\:.*?-->(\r\n|\n|\n\r)'si"),"",$data);
  30  }
  31  function Replace($vars,$clear = TRUE) {
  32  if (is_array($vars)) {
  33  foreach ($vars as $key => $var) {
  34  if (is_array($var)) {
  35  unset($vars[$key]);
  36  }                
  37  }            
  38  }
  39  $patterns = array();
  40  $replacements = array();
  41  if (is_array($vars))
  42  foreach ($vars as $key => $val) {
  43  $patterns[] = "/\{" . strtoupper($key) . "\}/";
  44  $replacements[] = str_replace('$','\$',$val);
  45  }
  46  $result = $this->output = preg_replace($patterns,$replacements,$this->input);
  47  if ($clear == TRUE)
  48  $this->Clear();
  49  return $result;
  50  }
  51  function ReplaceSingle($var,$value,$perm = FALSE) {
  52  $var = strtoupper($var);
  53  if ($perm == TRUE)
  54  $this->input = str_replace("\{$var}",$value,$this->input);
  55  return $this->output = str_replace("\{$var}",$value,$this->output);
  56  }
  57  function Clear() {
  58  $this->output = $this->input;
  59  }
  60  function EmptyVars() {
  61  global $_TSM;
  62  return $this->output = preg_replace("'{[A-Z_\-0-9]*?}'si","",$this->output);
  63  }
  64  function BlockExists($block_name) {
  65  return isset($this->blocks[$block_name]) ? TRUE : FALSE;
  66  }
  67  }
  68  class CLayout extends CXMLParser {
  69  var $data;
  70  var $version;
  71  var $name;
  72  var $base;
  73  var $body;
  74  var $blocks;
  75  var $loaded = FALSE;
  76  function CLayout($file_name = "") {
  77  parent::CXMLParser();
  78  if ($file_name != "")
  79  $this->Load($file_name);
  80  }
  81  function HNDTagOpen($parser,$tag,$attr) {
  82  global $base;
  83  switch ($tag) {
  84  case "LAYOUT":
  85  $this->version = ($attr["VERSION"] == "") ? "1.0" : $attr["VERSION"];
  86  $this->base = $attr["BASE"];
  87  $this->name = $attr["NAME"];
  88  $this->body = new CTemplate($GLOBALS["_TEMPLATES_PATH"] . $this->base . $attr["SRC"]);
  89  break;
  90  case "BLOCK":
  91  $this->blocks[$attr["NAME"]] = new CTemplate($GLOBALS["_TEMPLATES_PATH"] . $this->base . $attr["SRC"]);
  92  break;
  93  case "ASSIGN":
  94  switch ($attr["TYPE"]) {
  95  case "var":
  96      if ($attr["BLOCK"] == $this->name)
  97          $this->body->ReplaceSingle($attr["VAR"],$attr["VAL"],TRUE);
  98      else
  99          $this->blocks[$attr["BLOCK"]]->ReplaceSingle($attr["VAR"],$attr["VAL"]);
 100  break;
 101  case "tpl":
 102      $parse = TRUE;
 103      if ($attr["REQUIRES"] != "")
 104          if ($GLOBALS["_TSM"][$attr["REQUIRES"]] == "")
 105              $parse = FALSE;
 106      if ($parse)
 107          $val = GetFileContents($this->base . $attr["VAL"],TRUE);
 108      else
 109          $val = $attr["DEFAULT"];
 110      if ($attr["BLOCK"] == $this->name)
 111          $this->body->ReplaceSingle($attr["VAR"],$val,TRUE);
 112      else
 113          $this->blocks[$attr["BLOCK"]]->ReplaceSingle($attr["VAR"],$val,TRUE);
 114  break;
 115  case "call":
 116      if ($attr["BLOCK"] == $this->name)
 117          $this->body->ReplaceSingle($attr["VAR"],call_user_func($attr["VAL"]),TRUE);
 118      else
 119          $this->blocks[$attr["BLOCK"]]->ReplaceSingle($attr["VAR"],call_user_func($attr["VAL"]));
 120  break;
 121  case "module":
 122      if (strstr($attr["PARAMS"],",")) {
 123          $params = "," . $attr["PARAMS"];
 124          $call = "\$result = call_user_func(array(&\$base->modules[\$attr[\"MODULE\"]],\$attr[\"METHOD\"])$params);";
 125          echo $call;
 126          eval($call);
 127      } else {
 128          $call = "\$result = \$base->modules[$attr[MODULE]]->$attr[METHOD]($attr[PARAMS]);";
 129          eval($call);                            
 130          }
 131      if ($attr["BLOCK"] == $this->name)
 132          $this->body->ReplaceSingle($attr["VAR"],$result,TRUE);
 133      else
 134          $this->blocks[$attr["BLOCK"]]->ReplaceSingle($attr["VAR"],$result,TRUE);
 135  break;
 136  }
 137  break;
 138  }
 139  }
 140  function Load($file_name) {
 141  if (file_exists($file_name)) {
 142  parent::Parse($this->data = GetFileContents($file_name));
 143  $this->loaded = TRUE;
 144  } else
 145  $this->loaded = FALSE;
 146  }
 147  function Replace($vars) {
 148  $this->body->Replace($vars,FALSE);
 149  }
 150  function BlockReplace($block,$vars) {
 151  $this->blocks[$block]->Replace($vars,FALSE);
 152  }
 153  function BlockExists($block) {
 154  if (isset($this->blocks[$block]))
 155  return TRUE;
 156  else
 157  return FALSE;
 158  }
 159  function Build() {
 160  global $_TSM;
 161  if ($this->loaded == FALSE)
 162  return;
 163  $vars = array();
 164  if (is_array($this->blocks)) {
 165  foreach ($this->blocks as $key => $block) {
 166  if ($_TSM[$key])
 167  $this->BlockReplace($key,$_TSM[$key]);
 168  $vars[$key] = $block->output;
 169  }
 170  foreach ($_TSM as $k => $v)
 171  if (!$this->BlockExists($k))
 172  $vars[$k] = $v;
 173  $vars = array_merge($vars,$_TSM["_PERM"]);
 174  $this->body->Replace($vars,FALSE);
 175  } else
 176  $this->body->Replace($_TSM,FALSE);
 177  }
 178  function Show() {
 179  print($this->body->output);
 180  }
 181  }
 182  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]