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