smarty_internal_compile_block.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Block
  4. *
  5. * Compiles the {block}{/block} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Block Class
  13. */
  14. class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {block} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return boolean true
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('name');
  26. $this->optional_attributes = array('assign', 'nocache');
  27. // check and get attributes
  28. $_attr = $this->_get_attributes($args);
  29. $save = array($_attr, $compiler->parser->current_buffer, $this->compiler->nocache);
  30. $this->_open_tag('block', $save);
  31. if (isset($_attr['nocache'])) {
  32. if ($_attr['nocache'] == 'true') {
  33. $compiler->nocache = true;
  34. }
  35. }
  36. $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
  37. $compiler->has_code = false;
  38. return true;
  39. }
  40. }
  41. /**
  42. * Smarty Internal Plugin Compile BlockClose Class
  43. */
  44. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
  45. /**
  46. * Compiles code for the {/block} tag
  47. *
  48. * @param array $args array with attributes from parser
  49. * @param object $compiler compiler object
  50. * @return string compiled code
  51. */
  52. public function compile($args, $compiler)
  53. {
  54. $this->compiler = $compiler;
  55. $this->smarty = $compiler->smarty;
  56. $this->compiler->has_code = true;
  57. // check and get attributes
  58. $this->optional_attributes = array('name');
  59. $_attr = $this->_get_attributes($args);
  60. $saved_data = $this->_close_tag(array('block'));
  61. // if name does match to opening tag
  62. if (isset($_attr['name']) && $saved_data[0]['name'] != $_attr['name']) {
  63. $this->compiler->trigger_template_error('mismatching name attributes "' . $saved_data[0]['name'] . '" and "' . $_attr['name'] . '"');
  64. }
  65. $_name = trim($saved_data[0]['name'], "\"'");
  66. if (isset($this->smarty->block_data[$_name])) {
  67. $_tpl = $this->smarty->createTemplate('string:' . $this->smarty->block_data[$_name]['source'], null, null, $compiler->template);
  68. $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  69. $_tpl->template_filepath = $this->smarty->block_data[$_name]['file'];
  70. if ($compiler->nocache) {
  71. $_tpl->forceNocache = 2;
  72. } else {
  73. $_tpl->forceNocache = 1;
  74. }
  75. $_tpl->suppressHeader = true;
  76. $_tpl->suppressFileDependency = true;
  77. if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  78. $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->getCompiledTemplate());
  79. } elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') {
  80. $_output = $_tpl->getCompiledTemplate() . $compiler->parser->current_buffer->to_smarty_php();
  81. } elseif ($this->smarty->block_data[$_name]['mode'] == 'append') {
  82. $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->getCompiledTemplate();
  83. } elseif (!empty($this->smarty->block_data[$_name])) {
  84. $_output = $_tpl->getCompiledTemplate();
  85. }
  86. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
  87. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
  88. if ($_tpl->has_nocache_code) {
  89. $compiler->template->has_nocache_code = true;
  90. }
  91. foreach($_tpl->required_plugins as $code => $tmp1) {
  92. foreach($tmp1 as $name => $tmp) {
  93. foreach($tmp as $type => $data) {
  94. $compiler->template->required_plugins[$code][$name][$type] = $data;
  95. }
  96. }
  97. }
  98. unset($_tpl);
  99. } else {
  100. $_output = $compiler->parser->current_buffer->to_smarty_php();
  101. }
  102. $compiler->parser->current_buffer = $saved_data[1];
  103. $compiler->nocache = $saved_data[2];
  104. // $_output content has already nocache code processed
  105. $compiler->suppressNocacheProcessing = true;
  106. return $_output;
  107. }
  108. }
  109. ?>