smarty_internal_compile_function.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Function
  4. *
  5. * Compiles the {function} {/function} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Function Class
  13. */
  14. class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {function} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return boolean true
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('name');
  26. $this->optional_attributes = array('_any');
  27. // check and get attributes
  28. $_attr = $this->_get_attributes($args);
  29. $save = array($_attr, $compiler->parser->current_buffer,
  30. $compiler->template->has_nocache_code, $compiler->template->required_plugins);
  31. $this->_open_tag('function', $save);
  32. $_name = trim($_attr['name'], "'\"");
  33. unset($_attr['name']);
  34. $compiler->template->properties['function'][$_name]['parameter'] = array();
  35. foreach ($_attr as $_key => $_data) {
  36. $compiler->template->properties['function'][$_name]['parameter'][$_key] = $_data;
  37. }
  38. $compiler->smarty->template_functions[$_name]['parameter'] = $compiler->template->properties['function'][$_name]['parameter'];
  39. if ($compiler->template->caching) {
  40. $output = '';
  41. } else {
  42. $output = "<?php if (!function_exists('smarty_template_function_{$_name}')) {
  43. function smarty_template_function_{$_name}(\$_smarty_tpl,\$params) {
  44. \$saved_tpl_vars = \$_smarty_tpl->tpl_vars;
  45. foreach (\$params as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);}?>";
  46. }
  47. // Init temporay context
  48. $compiler->template->required_plugins = array('compiled' => array(), 'nocache' => array());
  49. $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
  50. $compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
  51. $compiler->template->has_nocache_code = false;
  52. $compiler->has_code = false;
  53. $compiler->template->properties['function'][$_name]['compiled'] = '';
  54. return true;
  55. }
  56. }
  57. /**
  58. * Smarty Internal Plugin Compile Functionclose Class
  59. */
  60. class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase {
  61. /**
  62. * Compiles code for the {/function} tag
  63. *
  64. * @param array $args array with attributes from parser
  65. * @param object $compiler compiler object
  66. * @return boolean true
  67. */
  68. public function compile($args, $compiler)
  69. {
  70. $this->compiler = $compiler;
  71. $_attr = $this->_get_attributes($args);
  72. $saved_data = $this->_close_tag(array('function'));
  73. $_name = trim($saved_data[0]['name'], "'\"");
  74. // build plugin include code
  75. $plugins_string = '';
  76. if (!empty($compiler->template->required_plugins['compiled'])) {
  77. $plugins_string = '<?php ';
  78. foreach($compiler->template->required_plugins['compiled'] as $tmp) {
  79. foreach($tmp as $data) {
  80. $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
  81. }
  82. }
  83. $plugins_string .= '?>';
  84. }
  85. if (!empty($compiler->template->required_plugins['nocache'])) {
  86. $plugins_string .= "<?php echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
  87. foreach($compiler->template->required_plugins['nocache'] as $tmp) {
  88. foreach($tmp as $data) {
  89. $plugins_string .= "if (!is_callable(\'{$data['function']}\')) include \'{$data['file']}\';\n";
  90. }
  91. }
  92. $plugins_string .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';?>\n";
  93. }
  94. // if caching save template function for possible nocache call
  95. if ($compiler->template->caching) {
  96. $compiler->template->properties['function'][$_name]['compiled'] .= $plugins_string
  97. . $compiler->parser->current_buffer->to_smarty_php();
  98. $compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  99. $compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
  100. $compiler->smarty->template_functions[$_name] = $compiler->template->properties['function'][$_name];
  101. $compiler->has_code = false;
  102. $output = true;
  103. } else {
  104. $output = $plugins_string . $compiler->parser->current_buffer->to_smarty_php() . "<?php \$_smarty_tpl->tpl_vars = \$saved_tpl_vars;}}?>\n";
  105. }
  106. // restore old compiler status
  107. $compiler->parser->current_buffer = $saved_data[1];
  108. $compiler->template->has_nocache_code = $compiler->template->has_nocache_code | $saved_data[2];
  109. $compiler->template->required_plugins = $saved_data[3];
  110. return $output;
  111. }
  112. }
  113. ?>