|
1 | 1 | <?php |
| 2 | + |
2 | 3 | /** |
3 | 4 | * Slim Framework (https://slimframework.com) |
4 | 5 | * |
|
16 | 17 |
|
17 | 18 | class PhpRenderer |
18 | 19 | { |
| 20 | + protected string $templatePath; |
| 21 | + |
| 22 | + protected array $attributes; |
| 23 | + |
| 24 | + protected string $layout; |
| 25 | + |
19 | 26 | /** |
20 | | - * @var string |
21 | | - */ |
22 | | - protected $templatePath; |
23 | | -/** |
24 | | - * @var array |
25 | | - */ |
26 | | - protected $attributes; |
27 | | -/** |
28 | | - * @var string |
29 | | - */ |
30 | | - protected $layout; |
31 | | -/** |
32 | 27 | * @param string $templatePath |
33 | 28 | * @param array $attributes |
34 | 29 | * @param string $layout |
@@ -67,16 +62,21 @@ public function getLayout(): string |
67 | 62 | /** |
68 | 63 | * @param string $layout |
69 | 64 | */ |
| 65 | + |
| 66 | + /** |
| 67 | + * @param string $layout |
| 68 | + * |
| 69 | + * @return void |
| 70 | + * |
| 71 | + * @throws PhpTemplateNotFoundException |
| 72 | + */ |
70 | 73 | public function setLayout(string $layout): void |
71 | 74 | { |
72 | | - if ($layout === '' || $layout === null) { |
73 | | - $this->layout = null; |
74 | | - } else { |
75 | | - if (!$this->templateExists($layout)) { |
76 | | - throw new PhpTemplateNotFoundException('Layout template "' . $layout . '" does not exist'); |
77 | | - } |
78 | | - $this->layout = $layout; |
| 75 | + if ($layout && !$this->templateExists($layout)) { |
| 76 | + throw new PhpTemplateNotFoundException('Layout template "' . $layout . '" does not exist'); |
79 | 77 | } |
| 78 | + |
| 79 | + $this->layout = $layout; |
80 | 80 | } |
81 | 81 |
|
82 | 82 | /** |
@@ -150,7 +150,7 @@ public function setTemplatePath(string $templatePath): void |
150 | 150 | public function fetch(string $template, array $data = [], bool $useLayout = false): string |
151 | 151 | { |
152 | 152 | $output = $this->fetchTemplate($template, $data); |
153 | | - if ($this->layout !== null && $useLayout) { |
| 153 | + if ($this->layout && $useLayout) { |
154 | 154 | $data['content'] = $output; |
155 | 155 | $output = $this->fetchTemplate($this->layout, $data); |
156 | 156 | } |
@@ -193,11 +193,11 @@ public function fetchTemplate(string $template, array $data = []): string |
193 | 193 | /** |
194 | 194 | * Returns true is template exists, false if not |
195 | 195 | * |
196 | | - * @param $template |
| 196 | + * @param string $template |
197 | 197 | * |
198 | 198 | * @return bool |
199 | 199 | */ |
200 | | - public function templateExists(string $template) : bool |
| 200 | + public function templateExists(string $template): bool |
201 | 201 | { |
202 | 202 | $path = $this->templatePath . $template; |
203 | 203 | return is_file($path) && is_readable($path); |
|
0 commit comments