/Backend/ThinkPHP/Lib/Think/Util/Widget.class.php

https://github.com/qhwang0427/backend_php · PHP · 83 lines · 29 code · 4 blank · 50 comment · 5 complexity · 1c598e230e0e581e810061cf868f0ce0 MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP Widget类 抽象类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. abstract class Widget extends Think {
  24. // 使用的模板引擎 每个Widget可以单独配置不受系统影响
  25. protected $template = '';
  26. /**
  27. +----------------------------------------------------------
  28. * 渲染输出 render方法是Widget唯一的接口
  29. * 使用字符串返回 不能有任何输出
  30. +----------------------------------------------------------
  31. * @access public
  32. +----------------------------------------------------------
  33. * @param mixed $data 要渲染的数据
  34. +----------------------------------------------------------
  35. * @return string
  36. +----------------------------------------------------------
  37. */
  38. abstract public function render($data);
  39. /**
  40. +----------------------------------------------------------
  41. * 渲染模板输出 供render方法内部调用
  42. +----------------------------------------------------------
  43. * @access public
  44. +----------------------------------------------------------
  45. * @param string $templateFile 模板文件
  46. * @param mixed $var 模板变量
  47. * @param string $charset 模板编码
  48. +----------------------------------------------------------
  49. * @return string
  50. +----------------------------------------------------------
  51. */
  52. protected function renderFile($templateFile='',$var='',$charset='utf-8') {
  53. ob_start();
  54. ob_implicit_flush(0);
  55. if(!file_exists_case($templateFile)){
  56. // 自动定位模板文件
  57. $name = substr(get_class($this),0,-6);
  58. $filename = empty($templateFile)?$name:$templateFile;
  59. $templateFile = LIB_PATH.'Widget/'.$name.'/'.$filename.C('TMPL_TEMPLATE_SUFFIX');
  60. if(!file_exists_case($templateFile))
  61. throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
  62. }
  63. $template = $this->template?$this->template:strtolower(C('TMPL_ENGINE_TYPE')?C('TMPL_ENGINE_TYPE'):'php');
  64. if('php' == $template) {
  65. // 使用PHP模板
  66. if(!empty($var)) extract($var, EXTR_OVERWRITE);
  67. // 直接载入PHP模板
  68. include $templateFile;
  69. }else{
  70. $className = 'Template'.ucwords($template);
  71. require_cache(THINK_PATH.'/Lib/Think/Util/Template/'.$className.'.class.php');
  72. $tpl = new $className;
  73. $tpl->fetch($templateFile,$var,$charset);
  74. }
  75. $content = ob_get_clean();
  76. return $content;
  77. }
  78. }
  79. ?>