Хранилища Subversion ant

Редакция

Содержимое файла | Последнее изменение | Открыть журнал | RSS

Редакция Автор № строки Строка
304 alex-w 1
<?php
2
/**
3
* Smarty Internal Plugin Compile Foreach
4
*
5
* Compiles the {foreach} tag
6
*
7
* @package Smarty
8
* @subpackage Compiler
9
* @author Uwe Tews
10
*/
11
/**
12
* Smarty Internal Plugin Compile Foreach Class
13
*/
14
class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
15
    /**
16
    * Compiles code for the {foreach} tag
17
    *
18
    * @param array $args array with attributes from parser
19
    * @param object $compiler compiler object
20
    * @return string compiled code
21
    */
22
    public function compile($args, $compiler)
23
    {
24
        $this->compiler = $compiler;
25
        $this->required_attributes = array('from', 'item');
26
        $this->optional_attributes = array('name', 'key');
27
        $tpl = $compiler->template;
28
        // check and get attributes
29
        $_attr = $this->_get_attributes($args);
30
 
31
        $this->_open_tag('foreach');
32
 
33
        $from = $_attr['from'];
34
        $item = $_attr['item'];
35
 
36
        if (isset($_attr['key'])) {
37
            $key = $_attr['key'];
38
        } else {
39
            $key = null;
40
        }
41
 
42
        if (isset($_attr['name'])) {
43
            $name = $_attr['name'];
44
            $has_name = true;
45
            $SmartyVarName = '$smarty.foreach.' . trim($name,'\'"') . '.';
46
        } else {
47
            $name = null;
48
            $has_name = false;
49
        }
50
        $ItemVarName = '$' . trim($item,'\'"') . '@';
51
        // evaluates which Smarty variables and properties have to be computed
52
        if ($has_name) {
53
            $usesSmartyFirst = strpos($tpl->template_source, $SmartyVarName . 'first') !== false;
54
            $usesSmartyLast = strpos($tpl->template_source, $SmartyVarName . 'last') !== false;
55
            $usesSmartyIndex = strpos($tpl->template_source, $SmartyVarName . 'index') !== false;
56
            $usesSmartyIteration = strpos($tpl->template_source, $SmartyVarName . 'iteration') !== false;
57
            $usesSmartyShow = strpos($tpl->template_source, $SmartyVarName . 'show') !== false;
58
            $usesSmartyTotal = $usesSmartyLast || strpos($tpl->template_source, $SmartyVarName . 'total') !== false;
59
        } else {
60
            $usesSmartyFirst = false;
61
            $usesSmartyLast = false;
62
            $usesSmartyTotal = false;
63
        }
64
 
65
        $usesPropFirst = $usesSmartyFirst || strpos($tpl->template_source, $ItemVarName . 'first') !== false;
66
        $usesPropLast = $usesSmartyLast || strpos($tpl->template_source, $ItemVarName . 'last') !== false;
67
        $usesPropIndex = $usesPropFirst || strpos($tpl->template_source, $ItemVarName . 'index') !== false;
68
        $usesPropIteration = $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'iteration') !== false;
69
        $usesPropShow = strpos($tpl->template_source, $ItemVarName . 'show') !== false;
70
        $usesPropTotal = $usesSmartyTotal || $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'total') !== false;
71
        // generate output code
72
        $output = "<?php ";
73
        $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
74
        if ($key != null) {
75
            $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
76
        }
77
        $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
78
        if ($usesPropTotal) {
79
            $output .= " \$_smarty_tpl->tpl_vars[$item]->total=count(\$_from);\n";
80
        }
81
        if ($usesPropIteration) {
82
            $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
83
        }
84
        if ($usesPropIndex) {
85
            $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
86
        }
87
        if ($has_name) {
88
            if ($usesSmartyTotal) {
89
                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
90
            }
91
            if ($usesSmartyIteration) {
92
                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
93
            }
94
            if ($usesSmartyIndex) {
95
                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
96
            }
97
        }
98
        $output .= "if (count(\$_from) > 0){\n";
99
        $output .= "    foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
100
        if ($key != null) {
101
            $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
102
        }
103
        if ($usesPropIteration) {
104
            $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
105
        }
106
        if ($usesPropIndex) {
107
            $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
108
        }
109
        if ($usesPropFirst) {
110
            $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
111
        }
112
        if ($usesPropLast) {
113
            $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
114
        }
115
        if ($has_name) {
116
            if ($usesSmartyFirst) {
117
                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
118
            }
119
            if ($usesSmartyIteration) {
120
                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
121
            }
122
            if ($usesSmartyIndex) {
123
                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
124
            }
125
            if ($usesSmartyLast) {
126
                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
127
            }
128
        }
129
        $output .= "?>";
130
 
131
        return $output;
132
    }
133
}
134
 
135
?>