Closed gdsmith closed 4 years ago
slim/php-view/src/PhpRenderer.php
method fetch
if ($this->layout !== null) {
ob_start();
$data['content'] = $output;
$this->protectedIncludeScope($this->layout, $data);
$output = ob_get_clean();
}
maybe the problem is here $this->layout == null if $this->layout == null - in this case object return only sub-template
README explains you can render sub-templates but doesn't say how:
\Slim\Views\PhpRenderer::fetch()
, as in <footer><?php echo $this->fetch('footer.phtml'); ?></footer>
, then you get Maximum function nesting level of '256' reached, aborting! breaking an infinite loop.Here's a potential patch I created which gives a workaround for template-less partials:
--- PhpRenderer.php.orig Mon Aug 19 15:37:38 2019
+++ PhpRenderer.php Mon Aug 19 15:39:37 2019
@@ -204,7 +204,7 @@
$this->protectedIncludeScope($this->templatePath . $template, $data);
$output = ob_get_clean();
- if ($this->layout !== null) {
+ if ($data['layout'] !== false && $data['layout'] !== '' && $this->layout !== null) {
ob_start();
$data['content'] = $output;
$this->protectedIncludeScope($this->layout, $data);
Usage:
echo $this->fetch('partials/test.phtml', [
'layout' => false,
]);
Another possibility would be to create a separate method which wouldn't include the layout, regardless (e.g.$this->fetchPartial('partials/test.phtml')
).
The cleanest way I came up with was extending the PhpRenderer
class and adding a fetchPartial()
method which is a copy of the partial()
method with the layout code removed.
Ideally, I think a method like this should be added to the actual project, but this will work for my use case in the mean time.
@jaywilliams
Thanks, I just suggest changing the flag to skipLayout
, so evaluation becomes easier:
- if ($this->layout !== null) {
+ if ($this->layout !== null && empty($data['skipLayout'])) {
If anyone is wondering about the status, bugfix #53 was implied to be a B/C feature and merged into 3.x branch.
From the documentation:
Including sub-templates (with
fetch
) when you are using layouts is problematic as sub-template content also includes the surrounding layout. Using a straight phpinclude(template.file)
is an effective workaround.