source: trunk/include/smarty/libs/sysplugins/smarty_internal_compile_private_print_expression.php @ 23384

Last change on this file since 23384 was 23384, checked in by rvelices, 11 years ago

smarty 3 - first pass for tests

File size: 6.6 KB
Line 
1<?php
2/**
3* Smarty Internal Plugin Compile Print Expression
4*
5* Compiles any tag which will output an expression or variable
6*
7* @package Smarty
8* @subpackage Compiler
9* @author Uwe Tews
10*/
11
12/**
13* Smarty Internal Plugin Compile Print Expression Class
14*
15* @package Smarty
16* @subpackage Compiler
17*/
18class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_CompileBase {
19
20    /**
21    * Attribute definition: Overwrites base class.
22    *
23    * @var array
24    * @see Smarty_Internal_CompileBase
25    */
26    public $optional_attributes = array('assign');
27    /**
28    * Attribute definition: Overwrites base class.
29    *
30    * @var array
31    * @see Smarty_Internal_CompileBase
32    */
33    public $option_flags = array('nocache', 'nofilter');
34
35    /**
36    * Compiles code for gererting output from any expression
37    *
38    * @param array  $args      array with attributes from parser
39    * @param object $compiler  compiler object
40    * @param array  $parameter array with compilation parameter
41    * @return string compiled code
42    */
43    public function compile($args, $compiler, $parameter)
44    {
45        // check and get attributes
46        $_attr = $this->getAttributes($compiler, $args);
47        // nocache option
48        if ($_attr['nocache'] === true) {
49            $compiler->tag_nocache = true;
50        }
51        // filter handling
52        if ($_attr['nofilter'] === true) {
53            $_filter = 'false';
54        } else {
55            $_filter = 'true';
56        }
57        if (isset($_attr['assign'])) {
58            // assign output to variable
59            $output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";
60        } else {
61            // display value
62            $output = $parameter['value'];
63            // tag modifier
64            if (!empty($parameter['modifierlist'])) {
65                $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $parameter['modifierlist'], 'value' => $output));
66            }
67            if (!$_attr['nofilter']) {
68                // default modifier
69                if (!empty($compiler->smarty->default_modifiers)) {
70                    if (empty($compiler->default_modifier_list)) {
71                        $modifierlist = array();
72                        foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
73                            preg_match_all('/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/', $single_default_modifier, $mod_array);
74                            for ($i = 0, $count = count($mod_array[0]);$i < $count;$i++) {
75                                if ($mod_array[0][$i] != ':') {
76                                    $modifierlist[$key][] = $mod_array[0][$i];
77                                }
78                            }
79                        }
80                        $compiler->default_modifier_list  = $modifierlist;
81                    }
82                    $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => $compiler->default_modifier_list, 'value' => $output));
83                }
84                // autoescape html
85                if ($compiler->template->smarty->escape_html) {
86                    $output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
87                }
88                // loop over registerd filters
89                if (!empty($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE])) {
90                    foreach ($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE] as $key => $function) {
91                        if (!is_array($function)) {
92                            $output = "{$function}({$output},\$_smarty_tpl)";
93                        } else if (is_object($function[0])) {
94                            $output = "\$_smarty_tpl->smarty->registered_filters[Smarty::FILTER_VARIABLE][{$key}][0]->{$function[1]}({$output},\$_smarty_tpl)";
95                        } else {
96                            $output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
97                        }
98                    }
99                }
100                // auto loaded filters
101                if (isset($compiler->smarty->autoload_filters[Smarty::FILTER_VARIABLE])) {
102                    foreach ((array)$compiler->template->smarty->autoload_filters[Smarty::FILTER_VARIABLE] as $name) {
103                        $result = $this->compile_output_filter($compiler, $name, $output);
104                        if ($result !== false) {
105                            $output = $result;
106                        } else {
107                            // not found, throw exception
108                            throw new SmartyException("Unable to load filter '{$name}'");
109                        }
110                    }
111                }
112                if (isset($compiler->template->variable_filters)) {
113                    foreach ($compiler->template->variable_filters as $filter) {
114                        if (count($filter) == 1 && ($result = $this->compile_output_filter($compiler, $filter[0], $output)) !== false) {
115                            $output = $result;
116                        } else {
117                            $output = $compiler->compileTag('private_modifier', array(), array('modifierlist' => array($filter), 'value' => $output));
118                        }
119                    }
120                }
121            }
122
123            $compiler->has_output = true;
124            $output = "<?php echo {$output};?>";
125        }
126        return $output;
127    }
128
129    /**
130    * @param object $compiler compiler object
131    * @param string $name     name of variable filter
132    * @param type   $output   embedded output
133    * @return string
134    */
135    private function compile_output_filter($compiler, $name, $output)
136    {
137        $plugin_name = "smarty_variablefilter_{$name}";
138        $path = $compiler->smarty->loadPlugin($plugin_name, false);
139        if ($path) {
140            if ($compiler->template->caching) {
141                $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
142                $compiler->template->required_plugins['nocache'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
143            } else {
144                $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['file'] = $path;
145                $compiler->template->required_plugins['compiled'][$name][Smarty::FILTER_VARIABLE]['function'] = $plugin_name;
146            }
147        } else {
148            // not found
149            return false;
150        }
151        return "{$plugin_name}({$output},\$_smarty_tpl)";
152    }
153
154}
155
156?>
Note: See TracBrowser for help on using the repository browser.