ContentReplaceBehavior.class.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 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. defined('THINK_PATH') or exit();
  12. /**
  13. * 系统行为扩展:模板内容输出替换
  14. * @category Think
  15. * @package Think
  16. * @subpackage Behavior
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class ContentReplaceBehavior extends Behavior {
  20. // 行为参数定义
  21. protected $options = array(
  22. 'TMPL_PARSE_STRING' => array(),
  23. );
  24. // 行为扩展的执行入口必须是run
  25. public function run(&$content){
  26. $content = $this->templateContentReplace($content);
  27. }
  28. /**
  29. * 模板内容替换
  30. * @access protected
  31. * @param string $content 模板内容
  32. * @return string
  33. */
  34. protected function templateContentReplace($content) {
  35. // 系统默认的特殊变量替换
  36. $replace = array(
  37. '__TMPL__' => APP_TMPL_PATH, // 项目模板目录
  38. '__ROOT__' => __ROOT__, // 当前网站地址
  39. '__APP__' => __APP__, // 当前项目地址
  40. '__GROUP__' => defined('GROUP_NAME')?__GROUP__:__APP__,
  41. '__ACTION__' => __ACTION__, // 当前操作地址
  42. '__SELF__' => __SELF__, // 当前页面地址
  43. '__URL__' => __URL__,
  44. '../Public' => APP_TMPL_PATH.'Public',// 项目公共模板目录
  45. '__PUBLIC__' => __ROOT__.'/Public',// 站点公共目录
  46. );
  47. // 允许用户自定义模板的字符串替换
  48. if(is_array(C('TMPL_PARSE_STRING')) )
  49. $replace = array_merge($replace,C('TMPL_PARSE_STRING'));
  50. $content = str_replace(array_keys($replace),array_values($replace),$content);
  51. return $content;
  52. }
  53. }