ContentReplaceBehavior.class.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Behavior;
  12. /**
  13. * 系统行为扩展:模板内容输出替换
  14. */
  15. class ContentReplaceBehavior
  16. {
  17. // 行为扩展的执行入口必须是run
  18. public function run(&$content)
  19. {
  20. $content = $this->templateContentReplace($content);
  21. }
  22. /**
  23. * 模板内容替换
  24. * @access protected
  25. * @param string $content 模板内容
  26. * @return string
  27. */
  28. protected function templateContentReplace($content)
  29. {
  30. // 系统默认的特殊变量替换
  31. $replace = array(
  32. '__ROOT__' => __ROOT__, // 当前网站地址
  33. '__APP__' => __APP__, // 当前应用地址
  34. '__MODULE__' => __MODULE__,
  35. '__ACTION__' => __ACTION__, // 当前操作地址
  36. '__SELF__' => htmlentities(__SELF__), // 当前页面地址
  37. '__CONTROLLER__' => __CONTROLLER__,
  38. '__URL__' => __CONTROLLER__,
  39. '__PUBLIC__' => __ROOT__ . '/Public', // 站点公共目录
  40. );
  41. // 允许用户自定义模板的字符串替换
  42. if (is_array(C('TMPL_PARSE_STRING'))) {
  43. $replace = array_merge($replace, C('TMPL_PARSE_STRING'));
  44. }
  45. $content = str_replace(array_keys($replace), array_values($replace), $content);
  46. return $content;
  47. }
  48. }