View.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace daswork;
  3. class View
  4. {
  5. // 视图实例
  6. protected static $instance;
  7. // // 模板引擎实例
  8. // public $engine;
  9. // 模板变量
  10. protected $data = [];
  11. // 用于静态赋值的模板变量
  12. protected static $var = [];
  13. // // 视图输出替换
  14. // protected $replace = [];
  15. // /**
  16. // * 构造函数
  17. // * @access public
  18. // * @param array $engine 模板引擎参数
  19. // * @param array $replace 字符串替换参数
  20. // */
  21. // public function __construct($engine = [], $replace = [])
  22. // {
  23. // //基础替换字符串
  24. // $request = Request::instance();
  25. // $base = $request->root();
  26. // $root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
  27. // if ('' != $root) {
  28. // $root = '/' . ltrim($root, '/');
  29. // }
  30. // $baseReplace = [
  31. // '__ROOT__' => $root,
  32. // '__URL__' => $base . '/' . $request->module() . '/' . Loader::parseName($request->controller()),
  33. // '__STATIC__' => $root . '/static',
  34. // '__CSS__' => $root . '/static/css',
  35. // '__JS__' => $root . '/static/js',
  36. // ];
  37. // $this->replace = array_merge($baseReplace, (array) $replace);
  38. // }
  39. // /**
  40. // * 初始化视图
  41. // * @access public
  42. // * @param array $engine 模板引擎参数
  43. // * @param array $replace 字符串替换参数
  44. // * @return object
  45. // */
  46. // public static function instance($engine = [], $replace = [])
  47. // {
  48. // if (is_null(self::$instance)) {
  49. // self::$instance = new self($engine, $replace);
  50. // }
  51. // return self::$instance;
  52. // }
  53. public static function instance()
  54. {
  55. if (is_null(self::$instance)) {
  56. self::$instance = new self();
  57. }
  58. return self::$instance;
  59. }
  60. /**
  61. * 变量名替换
  62. * @param $name
  63. * @param $value
  64. */
  65. public function assign($name, $value)
  66. {
  67. if (is_array($name)) {
  68. $this->data = array_merge($this->data, $name);
  69. } else {
  70. $this->data[$name] = $value;
  71. }
  72. return $this;
  73. }
  74. /**
  75. * 显示视图
  76. * @param $file
  77. */
  78. // public function display($file)
  79. // {
  80. // $file = APP.'/views/'.$file;
  81. // if (is_file($file)){
  82. // extract($this->assign);#从数组中将变量导入到当前的符号表
  83. // include $file;
  84. // }
  85. // }
  86. public function fetch($file = "")
  87. {
  88. // 页面缓存
  89. ob_start();
  90. ob_implicit_flush(0);
  91. if (!empty($this->data)) {
  92. extract($this->data);
  93. }
  94. if ($file) {
  95. $namearary = explode(".", $file);
  96. $typename = end($namearary);
  97. if (!in_array($typename, ['html', 'tpl', 'htm', 'php'])) {
  98. $file = $file . '.' . TPL;
  99. }
  100. $filename = APP_PATH . "/" . $GLOBALS['model'] . "/view/" . strtolower($GLOBALS['ctrl']) . "/" . $file;
  101. require($filename);
  102. } else {
  103. require(APP_PATH . DS . $GLOBALS['model'] . DS . 'view' . DS . strtolower($GLOBALS['ctrl']) . DS . $GLOBALS['action'] . TPL);
  104. }
  105. // 获取并清空缓存
  106. $content = ob_get_clean();
  107. return $content;
  108. }
  109. }