3 * Smarty Internal Plugin Compile Break
4 * Compiles the {break} tag
12 * Smarty Internal Plugin Compile Break Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase
20 * Attribute definition: Overwrites base class.
23 * @see Smarty_Internal_CompileBase
25 public $optional_attributes = array('levels');
28 * Attribute definition: Overwrites base class.
31 * @see Smarty_Internal_CompileBase
33 public $shorttag_order = array('levels');
36 * Compiles code for the {break} tag
38 * @param array $args array with attributes from parser
39 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
40 * @param array $parameter array with compilation parameter
42 * @return string compiled code
44 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
46 list($levels, $foreachLevels) = $this->checkLevels($args, $compiler);
49 /* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
50 $foreachCompiler = $compiler->getTagCompiler('foreach');
51 $output .= $foreachCompiler->compileRestore($foreachLevels);
53 $output .= "break {$levels};?>";
58 * check attributes and return array of break and foreach levels
60 * @param array $args array with attributes from parser
61 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
62 * @param string $tag tag name
65 * @throws \SmartyCompilerException
67 public function checkLevels($args, Smarty_Internal_TemplateCompilerBase $compiler, $tag = 'break')
69 static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true);
70 // check and get attributes
71 $_attr = $this->getAttributes($compiler, $args);
73 if ($_attr[ 'nocache' ] === true) {
74 $compiler->trigger_template_error('nocache option not allowed', null, true);
77 if (isset($_attr[ 'levels' ])) {
78 if (!is_numeric($_attr[ 'levels' ])) {
79 $compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
81 $levels = $_attr[ 'levels' ];
85 $level_count = $levels;
86 $stack_count = count($compiler->_tag_stack) - 1;
89 while ($level_count >= 0 && $stack_count >= 0) {
90 if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) {
91 $lastTag = $compiler->_tag_stack[ $stack_count ][ 0 ];
92 if ($level_count === 0) {
96 if ($compiler->_tag_stack[ $stack_count ][ 0 ] === 'foreach') {
102 if ($level_count != 0) {
103 $compiler->trigger_template_error("cannot {$tag} {$levels} level(s)", null, true);
105 if ($lastTag === 'foreach' && $tag === 'break') {
108 return array($levels, $foreachLevels);