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_runtime_codeframe.php 4.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Smarty Internal Extension
  4. * This file contains the Smarty template extension to create a code frame
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Class Smarty_Internal_Extension_CodeFrame
  12. * Create code frame for compiled and cached templates
  13. */
  14. class Smarty_Internal_Runtime_CodeFrame
  15. {
  16. /**
  17. * Create code frame for compiled and cached templates
  18. *
  19. * @param Smarty_Internal_Template $_template
  20. * @param string $content optional template content
  21. * @param string $functions compiled template function and block code
  22. * @param bool $cache flag for cache file
  23. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  24. *
  25. * @return string
  26. */
  27. public function create(Smarty_Internal_Template $_template, $content = '', $functions = '', $cache = false,
  28. Smarty_Internal_TemplateCompilerBase $compiler = null)
  29. {
  30. // build property code
  31. $properties[ 'has_nocache_code' ] = $_template->compiled->has_nocache_code;
  32. $properties[ 'version' ] = Smarty::SMARTY_VERSION;
  33. $properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
  34. if (!$cache) {
  35. $properties[ 'file_dependency' ] = $_template->compiled->file_dependency;
  36. $properties[ 'includes' ] = $_template->compiled->includes;
  37. if (!empty($compiler->tpl_function)) {
  38. $properties[ 'tpl_function' ] = $compiler->tpl_function;
  39. }
  40. } else {
  41. $properties[ 'file_dependency' ] = $_template->cached->file_dependency;
  42. $properties[ 'cache_lifetime' ] = $_template->cache_lifetime;
  43. if (!empty($_template->tpl_function)) {
  44. $properties[ 'tpl_function' ] = $_template->tpl_function;
  45. }
  46. }
  47. $output = "<?php\n";
  48. $output .= "/* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
  49. "\n from \"" . $_template->source->filepath . "\" */\n\n";
  50. $dec = "\$_smarty_tpl->smarty->ext->_validateCompiled->decodeProperties(\$_smarty_tpl, " .
  51. var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ")";
  52. $output .= "if ({$dec}) {\n";
  53. $output .= "function {$properties['unifunc']} (\$_smarty_tpl) {\n";
  54. // include code for plugins
  55. if (!$cache) {
  56. if (!empty($_template->compiled->required_plugins[ 'compiled' ])) {
  57. foreach ($_template->compiled->required_plugins[ 'compiled' ] as $tmp) {
  58. foreach ($tmp as $data) {
  59. $file = addslashes($data[ 'file' ]);
  60. if (is_array($data[ 'function' ])) {
  61. $output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n";
  62. } else {
  63. $output .= "if (!is_callable('{$data['function']}')) require_once '{$file}';\n";
  64. }
  65. }
  66. }
  67. }
  68. if ($_template->caching && !empty($_template->compiled->required_plugins[ 'nocache' ])) {
  69. $_template->compiled->has_nocache_code = true;
  70. $output .= "echo '/*%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
  71. foreach ($_template->compiled->required_plugins[ 'nocache' ] as $tmp) {
  72. foreach ($tmp as $data) {
  73. $file = addslashes($data[ 'file' ]);
  74. if (is_Array($data[ 'function' ])) {
  75. $output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n");
  76. } else {
  77. $output .= addslashes("if (!is_callable('{$data['function']}')) require_once '{$file}';\n");
  78. }
  79. }
  80. }
  81. $output .= "?>/*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/';\n";
  82. }
  83. }
  84. $output .= "?>\n";
  85. $output .= $content;
  86. $output .= "<?php }\n?>";
  87. $output .= $functions;
  88. $output .= "<?php }\n";
  89. // remove unneeded PHP tags
  90. return preg_replace(array('/\s*\?>[\n]?<\?php\s*/', '/\?>\s*$/'), array("\n", ''), $output);
  91. }
  92. }