Files
smarty/libs/sysplugins/smarty_internal_compile_private_print_expression.php

162 lines
7.2 KiB
PHP
Raw Normal View History

<?php
/**
* Smarty Internal Plugin Compile Print Expression
* Compiles any tag which will output an expression or variable
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
2010-08-17 15:39:51 +00:00
/**
* Smarty Internal Plugin Compile Print Expression Class
*
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_CompileBase
{
2011-09-16 14:19:56 +00:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
2011-09-16 14:19:56 +00:00
public $optional_attributes = array('assign');
2011-09-16 14:19:56 +00:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
2011-09-16 14:19:56 +00:00
public $option_flags = array('nocache', 'nofilter');
/**
* Compiles code for generating output from any expression
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
// check and get attributes
2011-09-16 14:19:56 +00:00
$_attr = $this->getAttributes($compiler, $args);
2016-02-09 01:27:15 +01:00
if (isset($_attr[ 'assign' ])) {
// assign output to variable
$output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";
} else {
// display value
2016-02-09 01:27:15 +01:00
$output = $parameter[ 'value' ];
2011-09-16 14:19:56 +00:00
// tag modifier
2016-02-09 01:27:15 +01:00
if (!empty($parameter[ 'modifierlist' ])) {
$output = $compiler->compileTag('private_modifier', array(),
array('modifierlist' => $parameter[ 'modifierlist' ],
'value' => $output));
2011-09-16 14:19:56 +00:00
}
2016-02-09 01:27:15 +01:00
if (!$_attr[ 'nofilter' ]) {
// default modifier
if (!empty($compiler->smarty->default_modifiers)) {
if (empty($compiler->default_modifier_list)) {
$modifierlist = array();
foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
2016-02-09 01:27:15 +01:00
preg_match_all('/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/',
$single_default_modifier, $mod_array);
for ($i = 0, $count = count($mod_array[ 0 ]); $i < $count; $i ++) {
if ($mod_array[ 0 ][ $i ] != ':') {
$modifierlist[ $key ][] = $mod_array[ 0 ][ $i ];
}
2011-09-16 14:19:56 +00:00
}
}
$compiler->default_modifier_list = $modifierlist;
2011-09-16 14:19:56 +00:00
}
2016-02-09 01:27:15 +01:00
$output = $compiler->compileTag('private_modifier', array(),
array('modifierlist' => $compiler->default_modifier_list,
'value' => $output));
2011-09-16 14:19:56 +00:00
}
// autoescape html
if ($compiler->template->smarty->escape_html) {
$output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
2011-09-16 14:19:56 +00:00
}
// loop over registered filters
2016-02-09 01:27:15 +01:00
if (!empty($compiler->template->smarty->registered_filters[ Smarty::FILTER_VARIABLE ])) {
foreach ($compiler->template->smarty->registered_filters[ Smarty::FILTER_VARIABLE ] as $key =>
$function) {
2011-09-16 14:19:56 +00:00
if (!is_array($function)) {
$output = "{$function}({$output},\$_smarty_tpl)";
2016-02-09 01:27:15 +01:00
} elseif (is_object($function[ 0 ])) {
$output =
"\$_smarty_tpl->smarty->registered_filters[Smarty::FILTER_VARIABLE]['{$key}'][0]->{$function[1]}({$output},\$_smarty_tpl)";
2011-09-16 14:19:56 +00:00
} else {
$output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
}
}
}
// auto loaded filters
2016-02-09 01:27:15 +01:00
if (isset($compiler->smarty->autoload_filters[ Smarty::FILTER_VARIABLE ])) {
foreach ((array) $compiler->template->smarty->autoload_filters[ Smarty::FILTER_VARIABLE ] as $name)
{
2011-09-16 14:19:56 +00:00
$result = $this->compile_output_filter($compiler, $name, $output);
if ($result !== false) {
$output = $result;
} else {
// not found, throw exception
throw new SmartyException("Unable to load filter '{$name}'");
}
}
}
foreach ($compiler->variable_filters as $filter) {
if (count($filter) == 1 &&
2016-02-09 01:27:15 +01:00
($result = $this->compile_output_filter($compiler, $filter[ 0 ], $output)) !== false
) {
$output = $result;
} else {
2016-02-09 01:27:15 +01:00
$output = $compiler->compileTag('private_modifier', array(),
array('modifierlist' => array($filter), 'value' => $output));
2011-09-16 14:19:56 +00:00
}
}
}
$compiler->has_output = true;
$output = "<?php echo {$output};?>";
2011-09-16 14:19:56 +00:00
}
return $output;
2011-09-16 14:19:56 +00:00
}
/**
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param string $name name of variable filter
* @param string $output embedded output
*
* @return string
*/
private function compile_output_filter(Smarty_Internal_TemplateCompilerBase $compiler, $name, $output)
2011-09-16 14:19:56 +00:00
{
$plugin_name = "smarty_variablefilter_{$name}";
$path = $compiler->smarty->loadPlugin($plugin_name, false);
if ($path) {
if ($compiler->template->caching) {
2016-02-09 01:27:15 +01:00
$compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'file' ] =
$path;
$compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'function' ] =
$plugin_name;
2011-09-16 14:19:56 +00:00
} else {
2016-02-09 01:27:15 +01:00
$compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'file' ] =
$path;
$compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $name ][ Smarty::FILTER_VARIABLE ][ 'function' ] =
$plugin_name;
2011-09-16 14:19:56 +00:00
}
} else {
// not found
return false;
}
2011-09-16 14:19:56 +00:00
return "{$plugin_name}({$output},\$_smarty_tpl)";
}
}