1
0

view.php 721 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. class View {
  3. protected static $included = [];
  4. /**
  5. * @param string $view
  6. * @param array $params
  7. * @throws Exception
  8. */
  9. public static function include(string $view, array $params = []) {
  10. $view_path = ROOT_DIR . "/views/" . $view . ".php";
  11. if(file_exists($view_path)) {
  12. self::$included[] = $view;
  13. extract($params);
  14. /** @noinspection PhpIncludeInspection */
  15. require $view_path;
  16. }
  17. else {
  18. throw new Exception("View \"$view\" not found");
  19. }
  20. }
  21. /**
  22. * @param string $view
  23. * @param array $params
  24. * @throws Exception
  25. */
  26. public static function includeOnce(string $view, array $params = []) {
  27. if(in_array($view, self::$included)) return;
  28. self::include($view, $params);
  29. }
  30. }