1<?php
2/**
3 * Smarty Internal Plugin Compile While
4 * Compiles the {while} tag
5 *
6 * @package    Smarty
7 * @subpackage Compiler
8 * @author     Uwe Tews
9 */
10
11/**
12 * Smarty Internal Plugin Compile While Class
13 *
14 * @package    Smarty
15 * @subpackage Compiler
16 */
17class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
18{
19    /**
20     * Compiles code for the {while} tag
21     *
22     * @param array                                 $args      array with attributes from parser
23     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
24     * @param array                                 $parameter array with compilation parameter
25     *
26     * @return string compiled code
27     * @throws \SmartyCompilerException
28     */
29    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
30    {
31        $compiler->loopNesting++;
32        // check and get attributes
33        $_attr = $this->getAttributes($compiler, $args);
34        $this->openTag($compiler, 'while', $compiler->nocache);
35        if (!array_key_exists('if condition', $parameter)) {
36            $compiler->trigger_template_error('missing while condition', null, true);
37        }
38        // maybe nocache because of nocache variables
39        $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
40        if (is_array($parameter[ 'if condition' ])) {
41            if ($compiler->nocache) {
42                // create nocache var to make it know for further compiling
43                if (is_array($parameter[ 'if condition' ][ 'var' ])) {
44                    $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
45                } else {
46                    $var = $parameter[ 'if condition' ][ 'var' ];
47                }
48                $compiler->setNocacheInVariable($var);
49            }
50            $prefixVar = $compiler->getNewPrefixVariable();
51            $assignCompiler = new Smarty_Internal_Compile_Assign();
52            $assignAttr = array();
53            $assignAttr[][ 'value' ] = $prefixVar;
54            if (is_array($parameter[ 'if condition' ][ 'var' ])) {
55                $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
56                $_output = "<?php while ({$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]}) {?>";
57                $_output .= $assignCompiler->compile(
58                    $assignAttr,
59                    $compiler,
60                    array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ])
61                );
62            } else {
63                $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ];
64                $_output = "<?php while ({$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]}) {?>";
65                $_output .= $assignCompiler->compile($assignAttr, $compiler, array());
66            }
67            return $_output;
68        } else {
69            return "<?php\n while ({$parameter['if condition']}) {?>";
70        }
71    }
72}
73
74/**
75 * Smarty Internal Plugin Compile Whileclose Class
76 *
77 * @package    Smarty
78 * @subpackage Compiler
79 */
80class Smarty_Internal_Compile_Whileclose extends Smarty_Internal_CompileBase
81{
82    /**
83     * Compiles code for the {/while} tag
84     *
85     * @param array                                 $args     array with attributes from parser
86     * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
87     *
88     * @return string compiled code
89     */
90    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
91    {
92        $compiler->loopNesting--;
93        // must endblock be nocache?
94        if ($compiler->nocache) {
95            $compiler->tag_nocache = true;
96        }
97        $compiler->nocache = $this->closeTag($compiler, array('while'));
98        return "<?php }?>\n";
99    }
100}
101