ContentReplaceBehavior.class.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // 行为扩展的执行入口必须是run
  17. public function run(&$content){
  18. $content = $this->templateContentReplace($content);
  19. }
  20. /**
  21. * 模板内容替换
  22. * @access protected
  23. * @param string $content 模板内容
  24. * @return string
  25. */
  26. protected function templateContentReplace($content) {
  27. // 系统默认的特殊变量替换
  28. $replace = array(
  29. '__ROOT__' => __ROOT__, // 当前网站地址
  30. '__APP__' => __APP__, // 当前应用地址
  31. '__MODULE__' => __MODULE__,
  32. '__ACTION__' => __ACTION__, // 当前操作地址
  33. '__SELF__' => htmlentities(__SELF__), // 当前页面地址
  34. '__CONTROLLER__'=> __CONTROLLER__,
  35. '__URL__' => __CONTROLLER__,
  36. '__PUBLIC__' => __ROOT__.'/Public',// 站点公共目录
  37. );
  38. // 允许用户自定义模板的字符串替换
  39. if(is_array(C('TMPL_PARSE_STRING')) )
  40. $replace = array_merge($replace,C('TMPL_PARSE_STRING'));
  41. $content = str_replace(array_keys($replace),array_values($replace),$content);
  42. return $content;
  43. }
  44. }