Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
15 / 18 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| File | |
83.33% |
15 / 18 |
|
75.00% |
3 / 4 |
9.37 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTemplate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| render | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| renderTemplate | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
4.59 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\View\Template; |
| 15 | |
| 16 | /** |
| 17 | * View file template class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\View |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 4.0.4 |
| 25 | */ |
| 26 | class File extends AbstractTemplate |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Constructor |
| 31 | * |
| 32 | * Instantiate the view file template object |
| 33 | * |
| 34 | * @param string $template |
| 35 | */ |
| 36 | public function __construct(string $template) |
| 37 | { |
| 38 | $this->setTemplate($template); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Set view template |
| 43 | * |
| 44 | * @param string $template |
| 45 | * @throws Exception |
| 46 | * @return static |
| 47 | */ |
| 48 | public function setTemplate(string $template): static |
| 49 | { |
| 50 | if (!file_exists($template)) { |
| 51 | throw new Exception("Error: The template file '" . $template . "' does not exist."); |
| 52 | } |
| 53 | $this->template = $template; |
| 54 | |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Render the view and return the output |
| 60 | * |
| 61 | * @param ?array $data |
| 62 | * @throws \Exception |
| 63 | * @return string |
| 64 | */ |
| 65 | public function render(?array $data = null): string |
| 66 | { |
| 67 | if ($data !== null) { |
| 68 | $this->data = $data; |
| 69 | } |
| 70 | $this->renderTemplate(); |
| 71 | return $this->output; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Render view template file |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | protected function renderTemplate(): void |
| 80 | { |
| 81 | if ($this->data !== null) { |
| 82 | foreach ($this->data as $key => $value) { |
| 83 | ${$key} = $value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | try { |
| 88 | ob_start(); |
| 89 | include $this->template; |
| 90 | $this->output = ob_get_clean(); |
| 91 | } catch (\Exception $e) { |
| 92 | ob_clean(); |
| 93 | throw $e; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | } |