slimphp / PHP-View

A Simple PHP Renderer for Slim 3 & 4 (or any other PSR-7 project)
MIT License
264 stars 60 forks source link

Add fetchTemplate() method for layout-less rendering #52

Closed jaywilliams closed 4 years ago

jaywilliams commented 5 years ago

When rendering partials, you often don't want it being wrapped in the template layout. See: #49

piotr-cz commented 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?

jaywilliams commented 4 years ago

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.

piotr-cz commented 4 years ago

Do you want to prepare new PR or should I?

akrabat commented 4 years ago

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.

akrabat commented 4 years ago

Rebased. Also renamed to fetchTemplate() and made fetch() use fetchTemplate().