smarty_internal_resource_extends.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource Extends
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Uwe Tews
  8. * @author Rodney Rehm
  9. */
  10. /**
  11. * Smarty Internal Plugin Resource Extends
  12. *
  13. * Implements the file system as resource for Smarty which {extend}s a chain of template files templates
  14. *
  15. * @package Smarty
  16. * @subpackage TemplateResources
  17. */
  18. class Smarty_Internal_Resource_Extends extends Smarty_Resource {
  19. /**
  20. * populate Source Object with meta data from Resource
  21. *
  22. * @param Smarty_Template_Source $source source object
  23. * @param Smarty_Internal_Template $_template template object
  24. */
  25. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
  26. {
  27. $uid = '';
  28. $sources = array();
  29. $components = explode('|', $source->name);
  30. $exists = true;
  31. foreach ($components as $component) {
  32. $s = Smarty_Resource::source(null, $source->smarty, $component);
  33. if ($s->type == 'php') {
  34. throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
  35. }
  36. $sources[$s->uid] = $s;
  37. $uid .= $s->filepath;
  38. if ($_template && $_template->smarty->compile_check) {
  39. $exists == $exists && $s->exists;
  40. }
  41. }
  42. $source->components = $sources;
  43. $source->filepath = $s->filepath;
  44. $source->uid = sha1($uid);
  45. if ($_template && $_template->smarty->compile_check) {
  46. $source->timestamp = $s->timestamp;
  47. $source->exists = $exists;
  48. }
  49. // need the template at getContent()
  50. $source->template = $_template;
  51. }
  52. /**
  53. * populate Source Object with timestamp and exists from Resource
  54. *
  55. * @param Smarty_Template_Source $source source object
  56. */
  57. public function populateTimestamp(Smarty_Template_Source $source)
  58. {
  59. $source->exists = true;
  60. foreach ($source->components as $s) {
  61. $source->exists == $source->exists && $s->exists;
  62. }
  63. $source->timestamp = $s->timestamp;
  64. }
  65. /**
  66. * Load template's source from files into current template object
  67. *
  68. * @param Smarty_Template_Source $source source object
  69. * @return string template source
  70. * @throws SmartyException if source cannot be loaded
  71. */
  72. public function getContent(Smarty_Template_Source $source)
  73. {
  74. if (!$source->exists) {
  75. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  76. }
  77. $_rdl = preg_quote($source->smarty->right_delimiter);
  78. $_ldl = preg_quote($source->smarty->left_delimiter);
  79. $_components = array_reverse($source->components);
  80. $_first = reset($_components);
  81. $_last = end($_components);
  82. foreach ($_components as $_component) {
  83. // register dependency
  84. if ($_component != $_first) {
  85. $source->template->properties['file_dependency'][$_component->uid] = array($_component->filepath, $_component->timestamp, $_component->type);
  86. }
  87. // read content
  88. $source->filepath = $_component->filepath;
  89. $_content = $_component->content;
  90. // extend sources
  91. if ($_component != $_last) {
  92. if (preg_match_all("!({$_ldl}block\s(.+?){$_rdl})!", $_content, $_open) !=
  93. preg_match_all("!({$_ldl}/block{$_rdl})!", $_content, $_close)) {
  94. throw new SmartyException("unmatched {block} {/block} pairs in template {$_component->type} '{$_component->name}'");
  95. }
  96. preg_match_all("!{$_ldl}block\s(.+?){$_rdl}|{$_ldl}/block{$_rdl}|{$_ldl}\*([\S\s]*?)\*{$_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  97. $_result_count = count($_result[0]);
  98. $_start = 0;
  99. while ($_start+1 < $_result_count) {
  100. $_end = 0;
  101. $_level = 1;
  102. if (substr($_result[0][$_start][0],0,strlen($source->smarty->left_delimiter)+1) == $source->smarty->left_delimiter.'*') {
  103. $_start++;
  104. continue;
  105. }
  106. while ($_level != 0) {
  107. $_end++;
  108. if (substr($_result[0][$_start + $_end][0],0,strlen($source->smarty->left_delimiter)+1) == $source->smarty->left_delimiter.'*') {
  109. continue;
  110. }
  111. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  112. $_level++;
  113. } else {
  114. $_level--;
  115. }
  116. }
  117. $_block_content = str_replace($source->smarty->left_delimiter . '$smarty.block.parent' . $source->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%', substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
  118. Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $source->template, $_component->filepath);
  119. $_start = $_start + $_end + 1;
  120. }
  121. } else {
  122. return $_content;
  123. }
  124. }
  125. }
  126. /**
  127. * Determine basename for compiled filename
  128. *
  129. * @param Smarty_Template_Source $source source object
  130. * @return string resource's basename
  131. */
  132. public function getBasename(Smarty_Template_Source $source)
  133. {
  134. return str_replace(':', '.', basename($source->filepath));
  135. }
  136. }
  137. ?>