smarty_internal_compile_extends.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_CompileBase
  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. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('file');
  32. /**
  33. * Compiles code for the {extends} tag
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param object $compiler compiler object
  37. *
  38. * @return string compiled code
  39. */
  40. public function compile($args, $compiler)
  41. {
  42. // check and get attributes
  43. $_attr = $this->getAttributes($compiler, $args);
  44. if ($_attr['nocache'] === true) {
  45. $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
  46. }
  47. if (strpos($_attr['file'], '$_tmp') !== false) {
  48. $compiler->trigger_template_error('illegal value for file attribute', $compiler->lex->taglineno);
  49. }
  50. $name = $_attr['file'];
  51. /** @var Smarty_Internal_Template $_smarty_tpl
  52. * used in evaluated code
  53. */
  54. $_smarty_tpl = $compiler->template;
  55. eval("\$tpl_name = $name;");
  56. // create template object
  57. $_template = new $compiler->smarty->template_class($tpl_name, $compiler->smarty, $compiler->template);
  58. // check for recursion
  59. $uid = $_template->source->uid;
  60. if (isset($compiler->extends_uid[$uid])) {
  61. $compiler->trigger_template_error("illegal recursive call of \"$include_file\"", $compiler->lex->line - 1);
  62. }
  63. $compiler->extends_uid[$uid] = true;
  64. if (empty($_template->source->components)) {
  65. array_unshift($compiler->sources, $_template->source);
  66. } else {
  67. foreach ($_template->source->components as $source) {
  68. array_unshift($compiler->sources, $source);
  69. $uid = $source->uid;
  70. if (isset($compiler->extends_uid[$uid])) {
  71. $compiler->trigger_template_error("illegal recursive call of \"{$source->filepath}\"", $compiler->lex->line - 1);
  72. }
  73. $compiler->extends_uid[$uid] = true;
  74. }
  75. }
  76. unset ($_template);
  77. $compiler->inheritance_child = true;
  78. $compiler->lex->yypushstate(Smarty_Internal_Templatelexer::CHILDBODY);
  79. return '';
  80. }
  81. }