Closed jaywilliams closed 4 years ago
Another way to fix the problem without adding new method could be that the render
method would pass flag to force layout rendering.
This way layout wouldn't be included when using fetch
method by default
public function render(ResponseInterface $response, $template, array $data = [])
{
$output = $this->fetch($template, $data, true);
//...
}
public function fetch($template, array $data = [], $useLayout = false) {
// ...
if ($this->layout !== null && $useLayout) {
//..
}
//...
}
What do you think?
I'm not opposed to that option, but if that were the case, I'd suggest adding a helper method of fetchPartial() which would set the $useLayout flag as true, like so:
public function fetchPartial($template, array $data = [], $useLayout = true) {
return $this->fetch($template, $data, $useLayout);
}
That way you have the best of both worlds, and less duplicate code.
EDIT: I misread your comment, what you're describing is exactly what I was preferring to. I totally agree with your comment.
Do you want to prepare new PR or should I?
I've taken #53 as a fix for #49, but I want to refactor based on this work as I think that we should have a method that renders one template only rather than fetch() rendering two.
Rebased. Also renamed to fetchTemplate()
and made fetch()
use fetchTemplate()
.
When rendering partials, you often don't want it being wrapped in the template layout. See: #49