| 1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
- namespace App;
- use Exception;
- use RuntimeException;
- class View {
- public static function include(string $name, array $parameters = []): void {
- try {
- $path = sprintf("%s/views/%s.php", $_ENV["DIR_ROOT"], $name);
- if(!file_exists($path)) {
- throw new RuntimeException("View '{$name}' not found.");
- }
- extract($parameters, EXTR_OVERWRITE);
- require $path;
- }
- catch(Exception $e) {
- ?>
- <div class="alert alert-danger mb-2">
- <i class="fa fa-fw fa-exclamation-circle"></i>
- <?=htmlspecialchars($e->getMessage())?>
- </div>
- <?php
- }
- }
- public static function getHtml(string $name, array $parameters): string {
- ob_start();
- self::include($name, $parameters);
- return ob_get_clean();
- }
- }
|