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_compile_extends.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile extend
  4. * Compiles the {extends} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile extend Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Extends extends Smarty_Internal_Compile_Shared_Inheritance
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('file');
  25. /**
  26. * Array of names of optional attribute required by tag
  27. * use array('_any') if there is no restriction of attributes names
  28. *
  29. * @var array
  30. */
  31. public $optional_attributes = array('extends_resource');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $shorttag_order = array('file');
  39. /**
  40. * Compiles code for the {extends} tag extends: resource
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  44. *
  45. * @return string compiled code
  46. * @throws \SmartyCompilerException
  47. * @throws \SmartyException
  48. */
  49. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  50. {
  51. // check and get attributes
  52. $_attr = $this->getAttributes($compiler, $args);
  53. if ($_attr[ 'nocache' ] === true) {
  54. $compiler->trigger_template_error('nocache option not allowed', $compiler->parser->lex->line - 1);
  55. }
  56. if (strpos($_attr[ 'file' ], '$_tmp') !== false) {
  57. $compiler->trigger_template_error('illegal value for file attribute', $compiler->parser->lex->line - 1);
  58. }
  59. // add code to initialize inheritance
  60. $this->registerInit($compiler, true);
  61. $file = trim($_attr[ 'file' ], '\'"');
  62. if (strlen($file) > 8 && substr($file, 0, 8) === 'extends:') {
  63. // generate code for each template
  64. $files = array_reverse(explode('|', substr($file, 8)));
  65. $i = 0;
  66. foreach ($files as $file) {
  67. if ($file[ 0 ] === '"') {
  68. $file = trim($file, '".');
  69. } else {
  70. $file = "'{$file}'";
  71. }
  72. $i ++;
  73. if ($i === count($files) && isset($_attr[ 'extends_resource' ])) {
  74. $this->compileEndChild($compiler);
  75. }
  76. $this->compileInclude($compiler, $file);
  77. }
  78. if (!isset($_attr[ 'extends_resource' ])) {
  79. $this->compileEndChild($compiler);
  80. }
  81. } else {
  82. $this->compileEndChild($compiler, $_attr[ 'file' ]);
  83. }
  84. $compiler->has_code = false;
  85. return '';
  86. }
  87. /**
  88. * Add code for inheritance endChild() method to end of template
  89. *
  90. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  91. * @param null|string $template optional inheritance parent template
  92. *
  93. * @throws \SmartyCompilerException
  94. * @throws \SmartyException
  95. */
  96. private function compileEndChild(Smarty_Internal_TemplateCompilerBase $compiler, $template = null)
  97. {
  98. $inlineUids = '';
  99. if (isset($template) && $compiler->smarty->merge_compiled_includes) {
  100. $code = $compiler->compileTag('include', array($template, array('scope' => 'parent')));
  101. if (preg_match('/([,][\s]*[\'][a-z0-9]+[\'][,][\s]*[\']content.*[\'])[)]/', $code, $match)) {
  102. $inlineUids = $match[ 1 ];
  103. }
  104. }
  105. $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
  106. '<?php $_smarty_tpl->inheritance->endChild($_smarty_tpl' .
  107. (isset($template) ?
  108. ", {$template}{$inlineUids}" :
  109. '') . ");\n?>");
  110. }
  111. /**
  112. * Add code for including subtemplate to end of template
  113. *
  114. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  115. * @param string $template subtemplate name
  116. *
  117. * @throws \SmartyCompilerException
  118. * @throws \SmartyException
  119. */
  120. private function compileInclude(Smarty_Internal_TemplateCompilerBase $compiler, $template)
  121. {
  122. $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
  123. $compiler->compileTag('include',
  124. array($template,
  125. array('scope' => 'parent'))));
  126. }
  127. /**
  128. * Create source code for {extends} from source components array
  129. *
  130. * @param \Smarty_Internal_Template $template
  131. *
  132. * @return string
  133. */
  134. public static function extendsSourceArrayCode(Smarty_Internal_Template $template)
  135. {
  136. $resources = array();
  137. foreach ($template->source->components as $source) {
  138. $resources[] = $source->resource;
  139. }
  140. return $template->smarty->left_delimiter . 'extends file=\'extends:' . join('|', $resources) .
  141. '\' extends_resource=true' . $template->smarty->right_delimiter;
  142. }
  143. }