1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: |
|
10: |
|
11: | |
12: | |
13: | |
14: | |
15: | |
16: |
|
17: | class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase
|
18: | {
|
19: | |
20: | |
21: | |
22: | |
23: | |
24: |
|
25: | public $optional_attributes = array('levels');
|
26: |
|
27: | |
28: | |
29: | |
30: | |
31: | |
32: |
|
33: | public $shorttag_order = array('levels');
|
34: |
|
35: | |
36: | |
37: | |
38: | |
39: |
|
40: | public $tag = 'break';
|
41: |
|
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: |
|
51: | public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
|
52: | {
|
53: | list($levels, $foreachLevels) = $this->checkLevels($args, $compiler);
|
54: | $output = "<?php ";
|
55: | if ($foreachLevels > 0 && $this->tag === 'continue') {
|
56: | $foreachLevels--;
|
57: | }
|
58: | if ($foreachLevels > 0) {
|
59: |
|
60: | $foreachCompiler = $compiler->getTagCompiler('foreach');
|
61: | $output .= $foreachCompiler->compileRestore($foreachLevels);
|
62: | }
|
63: | $output .= "{$this->tag} {$levels};?>";
|
64: | return $output;
|
65: | }
|
66: |
|
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: |
|
76: | public function checkLevels($args, Smarty_Internal_TemplateCompilerBase $compiler)
|
77: | {
|
78: | static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true);
|
79: |
|
80: | $_attr = $this->getAttributes($compiler, $args);
|
81: | if ($_attr[ 'nocache' ] === true) {
|
82: | $compiler->trigger_template_error('nocache option not allowed', null, true);
|
83: | }
|
84: | if (isset($_attr[ 'levels' ])) {
|
85: | if (!is_numeric($_attr[ 'levels' ])) {
|
86: | $compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
|
87: | }
|
88: | $levels = $_attr[ 'levels' ];
|
89: | } else {
|
90: | $levels = 1;
|
91: | }
|
92: | $level_count = $levels;
|
93: | $stack_count = count($compiler->_tag_stack) - 1;
|
94: | $foreachLevels = 0;
|
95: | $lastTag = '';
|
96: | while ($level_count > 0 && $stack_count >= 0) {
|
97: | if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) {
|
98: | $lastTag = $compiler->_tag_stack[ $stack_count ][ 0 ];
|
99: | if ($level_count === 0) {
|
100: | break;
|
101: | }
|
102: | $level_count--;
|
103: | if ($compiler->_tag_stack[ $stack_count ][ 0 ] === 'foreach') {
|
104: | $foreachLevels++;
|
105: | }
|
106: | }
|
107: | $stack_count--;
|
108: | }
|
109: | if ($level_count !== 0) {
|
110: | $compiler->trigger_template_error("cannot {$this->tag} {$levels} level(s)", null, true);
|
111: | }
|
112: | if ($lastTag === 'foreach' && $this->tag === 'break' && $foreachLevels > 0) {
|
113: | $foreachLevels--;
|
114: | }
|
115: | return array($levels, $foreachLevels);
|
116: | }
|
117: | }
|
118: | |