smarty_internal_parsetree.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Templateparser Parsetrees
  4. *
  5. * These are classes to build parsetrees in the template parser
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Thue Kristensen
  10. * @author Uwe Tews
  11. */
  12. abstract class _smarty_parsetree {
  13. abstract public function to_smarty_php();
  14. }
  15. /* A complete smarty tag. */
  16. class _smarty_tag extends _smarty_parsetree
  17. {
  18. public $parser;
  19. public $data;
  20. public $saved_block_nesting;
  21. function __construct($parser, $data)
  22. {
  23. $this->parser = $parser;
  24. $this->data = $data;
  25. $this->saved_block_nesting = $parser->block_nesting_level;
  26. }
  27. public function to_smarty_php()
  28. {
  29. return $this->data;
  30. }
  31. public function assign_to_var()
  32. {
  33. $var = sprintf('$_tmp%d', ++$this->parser->prefix_number);
  34. $this->parser->compiler->prefix_code[] = sprintf('<?php ob_start();?>%s<?php %s=ob_get_clean();?>',
  35. $this->data, $var);
  36. return $var;
  37. }
  38. }
  39. /* Code fragment inside a tag. */
  40. class _smarty_code extends _smarty_parsetree {
  41. public $parser;
  42. public $data;
  43. function __construct($parser, $data)
  44. {
  45. $this->parser = $parser;
  46. $this->data = $data;
  47. }
  48. public function to_smarty_php()
  49. {
  50. return sprintf("(%s)", $this->data);
  51. }
  52. }
  53. /* Double quoted string inside a tag. */
  54. class _smarty_doublequoted extends _smarty_parsetree {
  55. public $parser;
  56. public $subtrees = Array();
  57. function __construct($parser, _smarty_parsetree $subtree)
  58. {
  59. $this->parser = $parser;
  60. $this->subtrees[] = $subtree;
  61. if ($subtree instanceof _smarty_tag) {
  62. $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
  63. }
  64. }
  65. function append_subtree(_smarty_parsetree $subtree)
  66. {
  67. $last_subtree = count($this->subtrees)-1;
  68. if ($last_subtree >= 0 && $this->subtrees[$last_subtree] instanceof _smarty_tag && $this->subtrees[$last_subtree]->saved_block_nesting < $this->parser->block_nesting_level) {
  69. if ($subtree instanceof _smarty_code) {
  70. $this->subtrees[$last_subtree]->data .= '<?php echo ' . $subtree->data . ';?>';
  71. } elseif ($subtree instanceof _smarty_dq_content) {
  72. $this->subtrees[$last_subtree]->data .= '<?php echo "' . $subtree->data . '";?>';
  73. } else {
  74. $this->subtrees[$last_subtree]->data .= $subtree->data;
  75. }
  76. } else {
  77. $this->subtrees[] = $subtree;
  78. }
  79. if ($subtree instanceof _smarty_tag) {
  80. $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
  81. }
  82. }
  83. public function to_smarty_php()
  84. {
  85. $code = '';
  86. foreach ($this->subtrees as $subtree) {
  87. if ($code !== "") {
  88. $code .= ".";
  89. }
  90. if ($subtree instanceof _smarty_tag) {
  91. $more_php = $subtree->assign_to_var();
  92. } else {
  93. $more_php = $subtree->to_smarty_php();
  94. }
  95. $code .= $more_php;
  96. if (!$subtree instanceof _smarty_dq_content) {
  97. $this->parser->compiler->has_variable_string = true;
  98. }
  99. }
  100. return $code;
  101. }
  102. }
  103. /* Raw chars as part of a double quoted string. */
  104. class _smarty_dq_content extends _smarty_parsetree {
  105. public $data;
  106. function __construct($parser, $data)
  107. {
  108. $this->parser = $parser;
  109. $this->data = $data;
  110. }
  111. public function to_smarty_php()
  112. {
  113. return '"' . $this->data . '"';
  114. }
  115. }
  116. /* Template element */
  117. class _smarty_template_buffer extends _smarty_parsetree {
  118. public $subtrees = Array();
  119. function __construct($parser)
  120. {
  121. $this->parser = $parser;
  122. }
  123. function append_subtree(_smarty_parsetree $subtree)
  124. {
  125. $this->subtrees[] = $subtree;
  126. }
  127. public function to_smarty_php()
  128. {
  129. $code = '';
  130. for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key++) {
  131. if ($key + 2 < $cnt) {
  132. if ($this->subtrees[$key] instanceof _smarty_linebreak && $this->subtrees[$key + 1] instanceof _smarty_tag && $this->subtrees[$key + 1]->data == '' && $this->subtrees[$key + 2] instanceof _smarty_linebreak) {
  133. $key = $key + 1;
  134. continue;
  135. }
  136. if (substr($this->subtrees[$key]->data, -1) == '<' && $this->subtrees[$key + 1]->data == '' && substr($this->subtrees[$key + 2]->data, -1) == '?') {
  137. $key = $key + 2;
  138. continue;
  139. }
  140. }
  141. if (substr($code, -1) == '<') {
  142. $subtree = $this->subtrees[$key]->to_smarty_php();
  143. if (substr($subtree, 0, 1) == '?') {
  144. $code = substr($code, 0, strlen($code)-1) . '<<?php ?>?' . substr($subtree, 1);
  145. } elseif ($this->parser->asp_tags && substr($subtree, 0, 1) == '%') {
  146. $code = substr($code, 0, strlen($code)-1) . '<<?php ?>%' . substr($subtree, 1);
  147. } else {
  148. $code .= $subtree;
  149. }
  150. continue;
  151. }
  152. if ($this->parser->asp_tags && substr($code, -1) == '%') {
  153. $subtree = $this->subtrees[$key]->to_smarty_php();
  154. if (substr($subtree, 0, 1) == '>') {
  155. $code = substr($code, 0, strlen($code)-1) . '%<?php ?>>' . substr($subtree, 1);
  156. } else {
  157. $code .= $subtree;
  158. }
  159. continue;
  160. }
  161. if (substr($code, -1) == '?') {
  162. $subtree = $this->subtrees[$key]->to_smarty_php();
  163. if (substr($subtree, 0, 1) == '>') {
  164. $code = substr($code, 0, strlen($code)-1) . '?<?php ?>>' . substr($subtree, 1);
  165. } else {
  166. $code .= $subtree;
  167. }
  168. continue;
  169. }
  170. $code .= $this->subtrees[$key]->to_smarty_php();
  171. }
  172. return $code;
  173. }
  174. }
  175. /* template text */
  176. class _smarty_text extends _smarty_parsetree {
  177. public $data;
  178. function __construct($parser, $data)
  179. {
  180. $this->parser = $parser;
  181. $this->data = $data;
  182. }
  183. public function to_smarty_php()
  184. {
  185. return $this->data;
  186. }
  187. }
  188. /* template linebreaks */
  189. class _smarty_linebreak extends _smarty_parsetree {
  190. public $data;
  191. function __construct($parser, $data)
  192. {
  193. $this->parser = $parser;
  194. $this->data = $data;
  195. }
  196. public function to_smarty_php()
  197. {
  198. return $this->data;
  199. }
  200. }
  201. ?>