/template/Template.php

https://github.com/regality/zombie-core · PHP · 169 lines · 154 code · 15 blank · 0 comment · 47 complexity · 47fc078c7f9b3d1f550e88b4d88f476e MD5 · raw file

  1. <?php
  2. require_once(__DIR__ . "/../util/helper.php");
  3. abstract class Template {
  4. public $parts = array();
  5. public $lang;
  6. function __construct($file, $force_file = false, $compress = false) {
  7. $this->compress = $compress;
  8. if (file_exists($file)) {
  9. $this->template = file_get_contents($file);
  10. } else if ($force_file) {
  11. trigger_error("File does not exist: $file\n", E_USER_ERROR);
  12. } else {
  13. $this->template = $file;
  14. }
  15. $this->template .= ' ';
  16. $this->parseTemplate();
  17. }
  18. function parseTemplate() {
  19. $mode = 'string';
  20. $tag = '';
  21. $current_str = '';
  22. $template = $this->template;
  23. for ($i = 0, $c = strlen($this->template) - 1; $i < $c; ++$i) {
  24. $char = $this->template[$i];
  25. $next_char = $this->template[$i + 1];
  26. if ($mode == 'string') {
  27. if ($char == '<' && $next_char == ':') {
  28. $mode = 'tag';
  29. $i += 1;
  30. $this->addString($current_str);
  31. $current_str = '';
  32. continue;
  33. } else {
  34. $current_str .= $char;
  35. }
  36. }
  37. if ($mode == 'tag') {
  38. if ($tag == '') {
  39. $params = '';
  40. if ($char == '*') {
  41. $continue = true;
  42. while ($continue) {
  43. if ($template[$i ] == '*' &&
  44. $template[$i + 1] == ':' &&
  45. $template[$i + 2] == '>')
  46. {
  47. if ($template[$i + 3] == "\n") {
  48. ++$i;
  49. }
  50. $continue = false;
  51. $i += 2;
  52. } else {
  53. $i++;
  54. }
  55. }
  56. $mode = 'string';
  57. continue;
  58. } else if ($char == '=') {
  59. $tag = 'echo';
  60. continue;
  61. } else if ($char == '?' && $next_char == '=') {
  62. $tag = 'cecho';
  63. ++$i;
  64. continue;
  65. } else {
  66. $tag = '';
  67. while (preg_match('/\s/', $template[$i])) {
  68. ++$i;
  69. }
  70. while (preg_match('/[a-zA-Z0-9_]/', $template[$i])) {
  71. $tag .= $template[$i];
  72. $i++;
  73. }
  74. $i--;
  75. }
  76. } else if ($char == ':' && $next_char == '>') {
  77. ++$i;
  78. $this->addTag($tag, $current_str);
  79. $current_str = '';
  80. $tag = '';
  81. $mode = 'string';
  82. } else {
  83. $current_str .= $char;
  84. }
  85. } // endif
  86. } //endfor
  87. if ($current_str) {
  88. $this->addString($current_str);
  89. }
  90. } //endfunction
  91. function addTag($tag, $contents) {
  92. $class = ucwords($tag) . "Tag" . strtoupper($this->lang);
  93. if (!class_exists($class)) {
  94. $class = "FunctionTag" . $this->lang;
  95. }
  96. $tag = new $class($tag, $contents, $this->compress);
  97. array_push($this->parts, $tag);
  98. }
  99. function addString($str) {
  100. $class = "TemplateString" . $this->lang;
  101. $str = new $class($str, $this->compress);
  102. array_push($this->parts, $str);
  103. }
  104. function render() {
  105. $str = '';
  106. foreach ($this->parts as $part) {
  107. $str .= $part;
  108. }
  109. return $str;
  110. }
  111. }
  112. class PhpToJs {
  113. public static $stack = array();
  114. static function pushVar($var, $replace) {
  115. array_push(PhpToJs::$stack, array($var, $replace));
  116. }
  117. static function popVar() {
  118. array_pop(PhpToJs::$stack);
  119. }
  120. static function replace($var) {
  121. foreach (PhpToJs::$stack as $replace) {
  122. $cvar = $replace[0];
  123. $nvar = $replace[1];
  124. if (strpos($var, $cvar) === 0 &&
  125. (strlen($var) === strlen($cvar) || !ctype_alpha($var[strlen($cvar)]) ) )
  126. {
  127. return str_replace($cvar, $nvar, $var);
  128. }
  129. }
  130. return $var;
  131. }
  132. static function varToJsAll($code) {
  133. $var_pat = "/\\$([a-zA-Z_][a-zA-Z0-9_'\"\[\]]*)/";
  134. $callback = function($matches) {
  135. return PhpToJs::varToJs($matches[1]);
  136. };
  137. return preg_replace_callback($var_pat, $callback, $code);
  138. }
  139. static function varToJs($var) {
  140. $var = trim($var, '$');
  141. $var = str_replace("']['", ".", $var);
  142. $var = str_replace("\"][\"", ".", $var);
  143. $var = str_replace("['", ".", $var);
  144. $var = str_replace("[\"", ".", $var);
  145. $var = str_replace("']", "", $var);
  146. $var = str_replace("\"]", "", $var);
  147. $var = str_replace("[$", "[", $var);
  148. $var = "data." . $var;
  149. $var = PhpToJs::replace($var);
  150. return $var;
  151. }
  152. }
  153. ?>