View.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace daswork;
  3. class View
  4. {
  5. public $assign;#用于判断类是否存在,节约性能
  6. /**
  7. * 变量名替换
  8. * @param $name
  9. * @param $value
  10. */
  11. public function assign($name, $value)
  12. {
  13. $this->assign[$name] = $value;
  14. }
  15. /**
  16. * 显示视图
  17. * @param $file
  18. */
  19. // public function display($file)
  20. // {
  21. // $file = APP.'/views/'.$file;
  22. // if (is_file($file)){
  23. // extract($this->assign);#从数组中将变量导入到当前的符号表
  24. // include $file;
  25. // }
  26. // }
  27. public function fetch($file = "")
  28. {
  29. if (!empty($this->assign)) {
  30. extract($this->assign);
  31. }
  32. if ($file) {
  33. $namearary = explode(".", $file);
  34. $typename = end($namearary);
  35. if (!in_array($typename, ['html','tpl','htm','php'])) {
  36. $file=$file . '.' . TPL;
  37. }
  38. $filename = APP_PATH."/".$GLOBALS['model']."/view/" . strtolower($GLOBALS['ctrl']) . "/".$file;
  39. // echo '<br/>'.$filename;
  40. return require($filename);
  41. } else {
  42. // var_dump(APP_PATH . DS . $GLOBALS['model'] . DS . 'view' . DS . strtolower($GLOBALS['ctrl']) . $GLOBALS['action']. TPL);
  43. return require(APP_PATH . DS . $GLOBALS['model'] . DS . 'view' . DS . strtolower($GLOBALS['ctrl']) . DS . $GLOBALS['action']. TPL);
  44. }
  45. }
  46. }