You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_parsetree_template.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Templateparser Parse Tree
  4. * These are classes to build parse tree in the template parser
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Thue Kristensen
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Template element
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. * @ignore
  17. */
  18. class Smarty_Internal_ParseTree_Template extends Smarty_Internal_ParseTree
  19. {
  20. /**
  21. * Array of template elements
  22. *
  23. * @var array
  24. */
  25. public $subtrees = Array();
  26. /**
  27. * Create root of parse tree for template elements
  28. *
  29. */
  30. public function __construct()
  31. {
  32. }
  33. /**
  34. * Append buffer to subtree
  35. *
  36. * @param \Smarty_Internal_Templateparser $parser
  37. * @param Smarty_Internal_ParseTree $subtree
  38. */
  39. public function append_subtree(Smarty_Internal_Templateparser $parser, Smarty_Internal_ParseTree $subtree)
  40. {
  41. if (!empty($subtree->subtrees)) {
  42. $this->subtrees = array_merge($this->subtrees, $subtree->subtrees);
  43. } else {
  44. if ($subtree->data !== '') {
  45. $this->subtrees[] = $subtree;
  46. }
  47. }
  48. }
  49. /**
  50. * Append array to subtree
  51. *
  52. * @param \Smarty_Internal_Templateparser $parser
  53. * @param \Smarty_Internal_ParseTree[] $array
  54. */
  55. public function append_array(Smarty_Internal_Templateparser $parser, $array = array())
  56. {
  57. if (!empty($array)) {
  58. $this->subtrees = array_merge($this->subtrees, (array) $array);
  59. }
  60. }
  61. /**
  62. * Prepend array to subtree
  63. *
  64. * @param \Smarty_Internal_Templateparser $parser
  65. * @param \Smarty_Internal_ParseTree[] $array
  66. */
  67. public function prepend_array(Smarty_Internal_Templateparser $parser, $array = array())
  68. {
  69. if (!empty($array)) {
  70. $this->subtrees = array_merge((array) $array, $this->subtrees);
  71. }
  72. }
  73. /**
  74. * Sanitize and merge subtree buffers together
  75. *
  76. * @param \Smarty_Internal_Templateparser $parser
  77. *
  78. * @return string template code content
  79. */
  80. public function to_smarty_php(Smarty_Internal_Templateparser $parser)
  81. {
  82. $code = '';
  83. for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key ++) {
  84. if ($this->subtrees[$key] instanceof Smarty_Internal_ParseTree_Text) {
  85. $subtree = $this->subtrees[$key]->to_smarty_php($parser);
  86. while ($key + 1 < $cnt && ($this->subtrees[$key + 1] instanceof Smarty_Internal_ParseTree_Text ||
  87. $this->subtrees[$key + 1]->data == '')) {
  88. $key ++;
  89. if ($this->subtrees[$key]->data == '') {
  90. continue;
  91. }
  92. $subtree .= $this->subtrees[$key]->to_smarty_php($parser);
  93. }
  94. if ($subtree == '') {
  95. continue;
  96. }
  97. $code .= preg_replace('/((<%)|(%>)|(<\?php)|(<\?)|(\?>)|(<\/?script))/', "<?php echo '\$1'; ?>\n",
  98. $subtree);
  99. continue;
  100. }
  101. if ($this->subtrees[$key] instanceof Smarty_Internal_ParseTree_Tag) {
  102. $subtree = $this->subtrees[$key]->to_smarty_php($parser);
  103. while ($key + 1 < $cnt && ($this->subtrees[$key + 1] instanceof Smarty_Internal_ParseTree_Tag ||
  104. $this->subtrees[$key + 1]->data == '')) {
  105. $key ++;
  106. if ($this->subtrees[$key]->data == '') {
  107. continue;
  108. }
  109. $subtree = $parser->compiler->appendCode($subtree, $this->subtrees[$key]->to_smarty_php($parser));
  110. }
  111. if ($subtree == '') {
  112. continue;
  113. }
  114. $code .= $subtree;
  115. continue;
  116. }
  117. $code .= $this->subtrees[$key]->to_smarty_php($parser);
  118. }
  119. return $code;
  120. }
  121. }