smarty_internal_compile_break.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Break
  4. *
  5. * Compiles the {break} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Break Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $optional_attributes = array('levels');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('levels');
  32. /**
  33. * Compiles code for the {break} tag
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param object $compiler compiler object
  37. * @param array $parameter array with compilation parameter
  38. * @return string compiled code
  39. */
  40. public function compile($args, $compiler, $parameter)
  41. {
  42. static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true);
  43. // check and get attributes
  44. $_attr = $this->getAttributes($compiler, $args);
  45. if ($_attr['nocache'] === true) {
  46. $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
  47. }
  48. if (isset($_attr['levels'])) {
  49. if (!is_numeric($_attr['levels'])) {
  50. $compiler->trigger_template_error('level attribute must be a numeric constant', $compiler->lex->taglineno);
  51. }
  52. $_levels = $_attr['levels'];
  53. } else {
  54. $_levels = 1;
  55. }
  56. $level_count = $_levels;
  57. $stack_count = count($compiler->_tag_stack) - 1;
  58. while ($level_count > 0 && $stack_count >= 0) {
  59. if (isset($_is_loopy[$compiler->_tag_stack[$stack_count][0]])) {
  60. $level_count--;
  61. }
  62. $stack_count--;
  63. }
  64. if ($level_count != 0) {
  65. $compiler->trigger_template_error("cannot break {$_levels} level(s)", $compiler->lex->taglineno);
  66. }
  67. $compiler->has_code = true;
  68. return "<?php break {$_levels}?>";
  69. }
  70. }
  71. ?>