1
0

View.php 737 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App;
  3. use Exception;
  4. use RuntimeException;
  5. class View {
  6. public static function include(string $name, array $parameters = []): void {
  7. try {
  8. $path = sprintf("%s/views/%s.php", $_ENV["DIR_ROOT"], $name);
  9. if(!file_exists($path)) {
  10. throw new RuntimeException("View '{$name}' not found.");
  11. }
  12. extract($parameters, EXTR_OVERWRITE);
  13. require $path;
  14. }
  15. catch(Exception $e) {
  16. ?>
  17. <div class="alert alert-danger mb-2">
  18. <i class="fa fa-fw fa-exclamation-circle"></i>
  19. <?=htmlspecialchars($e->getMessage())?>
  20. </div>
  21. <?php
  22. }
  23. }
  24. public static function getHtml(string $name, array $parameters): string {
  25. ob_start();
  26. self::include($name, $parameters);
  27. return ob_get_clean();
  28. }
  29. }