You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_private_print_expression.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * Smarty Internal Plugin Compile Print Expression Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_CompileBase
  17. {
  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. * Compiles code for generating output from any expression
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  37. * @param array $parameter array with compilation parameter
  38. *
  39. * @return string
  40. * @throws \SmartyException
  41. */
  42. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  43. {
  44. // check and get attributes
  45. $_attr = $this->getAttributes($compiler, $args);
  46. $output = $parameter[ 'value' ];
  47. // tag modifier
  48. if (!empty($parameter[ 'modifierlist' ])) {
  49. $output = $compiler->compileTag('private_modifier', array(),
  50. array('modifierlist' => $parameter[ 'modifierlist' ],
  51. 'value' => $output));
  52. }
  53. if (isset($_attr[ 'assign' ])) {
  54. // assign output to variable
  55. return "<?php \$_smarty_tpl->assign({$_attr['assign']},{$output});?>";
  56. } else {
  57. // display value
  58. if (!$_attr[ 'nofilter' ]) {
  59. // default modifier
  60. if (!empty($compiler->smarty->default_modifiers)) {
  61. if (empty($compiler->default_modifier_list)) {
  62. $modifierlist = array();
  63. foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
  64. preg_match_all('/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/',
  65. $single_default_modifier, $mod_array);
  66. for ($i = 0, $count = count($mod_array[ 0 ]); $i < $count; $i ++) {
  67. if ($mod_array[ 0 ][ $i ] !== ':') {
  68. $modifierlist[ $key ][] = $mod_array[ 0 ][ $i ];
  69. }
  70. }
  71. }
  72. $compiler->default_modifier_list = $modifierlist;
  73. }
  74. $output = $compiler->compileTag('private_modifier', array(),
  75. array('modifierlist' => $compiler->default_modifier_list,
  76. 'value' => $output));
  77. }
  78. // autoescape html
  79. if ($compiler->template->smarty->escape_html) {
  80. $output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
  81. }
  82. // loop over registered filters
  83. if (!empty($compiler->template->smarty->registered_filters[ Smarty::FILTER_VARIABLE ])) {
  84. foreach ($compiler->template->smarty->registered_filters[ Smarty::FILTER_VARIABLE ] as $key =>
  85. $function) {
  86. if (!is_array($function)) {
  87. $output = "{$function}({$output},\$_smarty_tpl)";
  88. } elseif (is_object($function[ 0 ])) {
  89. $output =
  90. "\$_smarty_tpl->smarty->registered_filters[Smarty::FILTER_VARIABLE]['{$key}'][0]->{$function[1]}({$output},\$_smarty_tpl)";
  91. } else {
  92. $output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
  93. }
  94. }
  95. }
  96. // auto loaded filters
  97. if (isset($compiler->smarty->autoload_filters[ Smarty::FILTER_VARIABLE ])) {
  98. foreach ((array) $compiler->template->smarty->autoload_filters[ Smarty::FILTER_VARIABLE ] as $name)
  99. {
  100. $result = $this->compile_variable_filter($compiler, $name, $output);
  101. if ($result !== false) {
  102. $output = $result;
  103. } else {
  104. // not found, throw exception
  105. throw new SmartyException("Unable to load variable filter '{$name}'");
  106. }
  107. }
  108. }
  109. foreach ($compiler->variable_filters as $filter) {
  110. if (count($filter) === 1 &&
  111. ($result = $this->compile_variable_filter($compiler, $filter[ 0 ], $output)) !== false
  112. ) {
  113. $output = $result;
  114. } else {
  115. $output = $compiler->compileTag('private_modifier', array(),
  116. array('modifierlist' => array($filter), 'value' => $output));
  117. }
  118. }
  119. }
  120. $output = "<?php echo {$output};?>\n";
  121. }
  122. return $output;
  123. }
  124. /**
  125. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  126. * @param string $name name of variable filter
  127. * @param string $output embedded output
  128. *
  129. * @return string
  130. * @throws \SmartyException
  131. */
  132. private function compile_variable_filter(Smarty_Internal_TemplateCompilerBase $compiler, $name, $output)
  133. {
  134. $function= $compiler->getPlugin($name, 'variablefilter');
  135. if ($function) {
  136. return "{$function}({$output},\$_smarty_tpl)";
  137. } else {
  138. // not found
  139. return false;
  140. }
  141. }
  142. }