/phpBB/includes/template_smarty.php

https://github.com/ckwalsh/phpBB-Smarty · PHP · 208 lines · 157 code · 30 blank · 21 comment · 12 complexity · db9904b19d2db77ca4a70c15430c64ad MD5 · raw file

  1. <?php
  2. /**
  3. * phpBB Smarty Compatibility Library
  4. * Written by Cullen Walsh
  5. * Released under the GNU General Public License
  6. **/
  7. require($phpbb_root_path . 'vendor/Smarty/libs/Smarty.class.php');
  8. /**
  9. * Smarty compiler function to handle language variables
  10. * If they are defined, it will directly place them into the template
  11. * If they are not explicitly defined, they will insert php to properly handle them
  12. **/
  13. function smarty_compiler_L($tag_arg, &$smarty)
  14. {
  15. global $user;
  16. $tag_arg = preg_replace('/[^A-Z_]*/', '', $tag_arg);
  17. if (isset($user->lang[$tag_arg]))
  18. {
  19. return "?>{$user->lang[$tag_arg]}<?php";
  20. }
  21. else
  22. {
  23. return 'echo(isset($this->_tpl_vars[\'L_' . $tag_arg . '\']) ? $this->_tpl_vars[\'L_' . $tag_arg . '\'] : \'{L ' . $tag_arg . '}\');';
  24. }
  25. }
  26. function smarty_compiler_LA($tag_arg, &$smarty)
  27. {
  28. global $user;
  29. $tag_arg = preg_replace('/[^A-Z_]*/', '', $tag_arg);
  30. if (isset($user->lang[$tag_arg]))
  31. {
  32. return '?>' . addslashes($user->lang[$tag_arg]) . '<?php';
  33. }
  34. else
  35. {
  36. return 'echo(isset($this->_tpl_vars[\'LA_' . $tag_arg . '\']) ? addslashes($this->_tpl_vars[\'LA_' . $tag_arg . '\']) : \'{LA ' . $tag_arg . '}\');';
  37. }
  38. }
  39. /**
  40. * Smarty post-filter
  41. * Removes extra php tags that only contain whitespace
  42. * These tags may be formed by the language replacements
  43. **/
  44. function smarty_postfilter_strip($compiled, &$smarty)
  45. {
  46. return preg_replace('/<\?php\s+\?>/', '', $compiled);
  47. }
  48. class TemplateSmarty
  49. {
  50. private $smarty;
  51. private $_files = array();
  52. private $_displayed_files = array();
  53. private $_block_vars = array();
  54. private $_last_blocks = array();
  55. function __construct()
  56. {
  57. global $phpbb_root_path;
  58. $this->smarty = new Smarty;
  59. $this->smarty->compile_dir = $phpbb_root_path . 'cache';
  60. $this->smarty->register_compiler_function('L', 'smarty_compiler_L', true);
  61. $this->smarty->register_compiler_function('LA', 'smarty_compiler_LA', true);
  62. $this->smarty->register_postfilter('smarty_postfilter_strip');
  63. }
  64. function set_template()
  65. {
  66. global $phpbb_root_path, $user;
  67. $this->smarty->template_dir = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
  68. $path = $user->theme['template_path'];
  69. /*
  70. if($user->theme['template_inherits_id'])
  71. {
  72. $path[] = $user->theme['template_inherits_path'];
  73. }
  74. */
  75. $this->smarty->compile_id = $path . '_' . $user->data['user_lang'];
  76. return true;
  77. }
  78. function set_custom_template($template_path, $template_name, $template_mode = 'template')
  79. {
  80. global $user;
  81. $this->smarty->template_dir = $template_path;
  82. $this->smarty->compile_id = $template_name . $user->data['user_lang'];
  83. return true;
  84. }
  85. function set_filenames($filename_array)
  86. {
  87. $this->_files = $filename_array + $this->_files;
  88. }
  89. function destroy()
  90. {
  91. $this->_files = array();
  92. $this->smarty = null;
  93. }
  94. function display($handle, $include_once = true)
  95. {
  96. global $user;
  97. if(!$include_once || !isset($this->displayed_files[$handle]))
  98. {
  99. $this->_setup_block_vars();
  100. $this->smarty->assign_by_ref('_user', $user);
  101. $this->smarty->display($this->_files[$handle]);
  102. $this->displayed_files[$handle] = true;
  103. }
  104. }
  105. function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
  106. {
  107. $output = '';
  108. if(!$include_once || !isset($this->displayed_files[$handle]))
  109. {
  110. $this->_setup_block_vars();
  111. $this->smarty->assign_by_ref('_user', $user);
  112. $output = $this->smarty->fetch($this->_files[$handle]);
  113. if(!empty($template_var))
  114. {
  115. $this->smarty->assign($template_var, $output);
  116. }
  117. $this->displayed_files[$handle] = true;
  118. }
  119. return ($return_content) ? $output : true;
  120. }
  121. function assign_vars($vararray)
  122. {
  123. $this->smarty->assign($vararray);
  124. }
  125. function assign_var($varname, $varval)
  126. {
  127. $this->smarty->assign($varname, $varval);
  128. }
  129. function assign_block_vars($blockname, $vararray)
  130. {
  131. $parts = explode('.', $blockname);
  132. $parent = $parts;
  133. $child = array_pop($parent);
  134. $parent = implode('.', $parent);
  135. if (empty($parent))
  136. {
  137. $block_count = 0;
  138. if(isset($this->_block_vars[$blockname]))
  139. {
  140. $block_count = sizeof($this->_block_vars[$blockname]);
  141. $this->_last_blocks[$blockname]['S_LAST_ROW'] = false;
  142. }
  143. else
  144. {
  145. $this->_block_vars[$blockname] = array();
  146. $vararray['S_FIRST_ROW'] = true;
  147. }
  148. $this->_block_vars[$blockname][$block_count] = $vararray;
  149. $this->_last_blocks[$blockname] = &$this->_block_vars[$blockname][$block_count];
  150. $this->_last_blocks[$blockname]['S_LAST_ROW'] = true;
  151. }
  152. else if(isset($this->_last_blocks[$parent]))
  153. {
  154. $block_count = 0;
  155. if(isset($this->_last_blocks[$parent][$child]))
  156. {
  157. $block_count = sizeof($this->_last_blocks[$parent][$child]);
  158. $this->_last_blocks[$blockname]['S_LAST_ROW'] = false;
  159. }
  160. else
  161. {
  162. $this->_last_blocks[$parent][$child] = array();
  163. $vararray['S_FIRST_ROW'] = true;
  164. }
  165. $this->_last_blocks[$parent][$child][$block_count] = $vararray;
  166. $this->_last_blocks[$blockname] = $this->_last_blocks[$parent][$child][$block_count];
  167. $this->_last_blocks[$blockname]['S_LAST_ROW'] = true;
  168. }
  169. }
  170. function _setup_block_vars()
  171. {
  172. $keys = array_keys($this->_block_vars);
  173. foreach($keys as $k)
  174. {
  175. $this->smarty->assign_by_ref($k, $this->_block_vars[$k]);
  176. }
  177. }
  178. }