1 <?php
2 /**
3 * Smarty Internal Plugin Compile If
4 *
5 * Compiles the {if} {else} {elseif} {/if} tags
6 *
7 * @package Smarty
8 * @subpackage Compiler
9 * @author Uwe Tews
10 */
11 /**
12 * Smarty Internal Plugin Compile If Class
13 */
14 class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase {
15 /**
16 * Compiles code for the {if} 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('if condition');
26 // check and get attributes
27 $_attr = $this->_get_attributes($args);
28 $this->_open_tag('if',array(1,$compiler->tag_nocache));
29 if (is_array($args['if condition'])) {
30 $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[".$args['if condition']['var']."])) \$_smarty_tpl->tpl_vars[".$args['if condition']['var']."] = new Smarty_Variable;";
31 $_output .= "if (\$_smarty_tpl->tpl_vars[".$args['if condition']['var']."]->value = ".$args['if condition']['value']."){?>";
32 return $_output;
33 } else {
34 return "<?php if ({$args['if condition']}){?>";
35 }
36 }
37 }
39 /**
40 * Smarty Internal Plugin Compile Else Class
41 */
42 class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase {
43 /**
44 * Compiles code for the {else} tag
45 *
46 * @param array $args array with attributes from parser
47 * @param object $compiler compiler object
48 * @return string compiled code
49 */
50 public function compile($args, $compiler)
51 {
52 $this->compiler = $compiler;
53 list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'elseif'));
54 $this->_open_tag('else',array($nesting,$compiler->tag_nocache));
56 return "<?php }else{ ?>";
57 }
58 }
60 /**
61 * Smarty Internal Plugin Compile ElseIf Class
62 */
63 class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase {
64 /**
65 * Compiles code for the {elseif} tag
66 *
67 * @param array $args array with attributes from parser
68 * @param object $compiler compiler object
69 * @return string compiled code
70 */
71 public function compile($args, $compiler)
72 {
73 $this->compiler = $compiler;
74 $this->required_attributes = array('if condition');
75 // check and get attributes
76 $_attr = $this->_get_attributes($args);
78 list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'elseif'));
80 if (empty($this->compiler->prefix_code)) {
81 $this->_open_tag('elseif', array($nesting, $compiler->tag_nocache));
82 return "<?php }elseif({$args['if condition']}){?>";
83 } else {
84 $tmp = '';
85 foreach ($this->compiler->prefix_code as $code) $tmp .= $code;
86 $this->compiler->prefix_code = array();
87 $this->_open_tag('elseif', array($nesting + 1, $compiler->tag_nocache));
88 return "<?php }else{?>{$tmp}<?php if ({$args['if condition']}){?>";
89 }
90 }
91 }
93 /**
94 * Smarty Internal Plugin Compile Ifclose Class
95 */
96 class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase {
97 /**
98 * Compiles code for the {/if} tag
99 *
100 * @param array $args array with attributes from parser
101 * @param object $compiler compiler object
102 * @return string compiled code
103 */
104 public function compile($args, $compiler)
105 {
106 $this->compiler = $compiler;
107 list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'else', 'elseif'));
108 $tmp = '';
109 for ($i = 0; $i < $nesting ; $i++) $tmp .= '}';
110 return "<?php {$tmp}?>";
111 }
112 }
114 ?>