slimphp / Twig-View

Slim Framework view helper built on top of the Twig templating component
http://slimframework.com
MIT License
356 stars 87 forks source link

twig->fetch() outputs to browser rather than return string #233

Closed CarlosNegrao closed 2 years ago

CarlosNegrao commented 2 years ago

I'm trying to use $output = $this->twig->fetch('template_file.twig', compact(...)); to return the generated HTML, but it only outputs straight to the browser.

I even tried adding die('test'); to make sure I'm calling the right method, but it only works when called before twig->fetch.

I've also tried ob_start() and ob_get_clean() before and after twig->render(), with the same results.

I'm using the latest stable 3.x branch available to composer, if it makes any difference. Is there anything else I could try?

l0gicgate commented 2 years ago

So it seems that $this->twig->fetch() returns Twig::render() instead of Twig::fetch()

public function fetch(string $template, array $data = []): string
{
    $data = array_merge($this->defaultVariables, $data);

    return $this->environment->render($template, $data);
}

https://github.com/slimphp/Twig-View/blob/a5edc133a6d58171ecccc4e1aa9491c061cf9152/src/Twig.php#L202-L206

I'm not sure if this is by design or an oversight. It does awfully look like an oversight from when we transferred from the 2.x branch to the 3.x branch.

A workaround for this at the moment would be to do the following:

$output = $this->twig->getEnvironment()->render('template.twig', []);
CarlosNegrao commented 2 years ago

$output = $this->twig->getEnvironment()->fetch('template.twig', []);

I just tried it, and got the error "Call to undefined method Twig\Environment::fetch()".

Checking the source code for Twig, Environment doesn't seem to have the method fetch, at least not on version 3.3.3 (I had just uupdated thinking it might have been the problem.), but I found a method Environment::display(), which also didn't work, so I went digging, reached TemplateWrapper::display(), which doesn't return anything, just calls Template::display(), which array returns nothing, just calls Template::displayWithErrorHandling(), which again returns nothing.

I've tried wrapping Environment::display() with ob_start() and ob_get_clean() and put a single "die('test')" after it to check if it would work, but no such luck.

Back to digging.

l0gicgate commented 2 years ago

I just tried it, and got the error "Call to undefined method Twig\Environment::fetch()".

I meant $this->twig->getEnvironment()->render(...) my bad

CarlosNegrao commented 2 years ago

Just tried it, nothing.

Looking at twig/src/Environment, it should work, but it didn't, so I expect this to be a problem with twig, either a bug or with the documentation, since I couldn't find anything about it other than old questions on stackoverflow.

l0gicgate commented 2 years ago

Ensure you don't use ob_start() or ob_get_clean() before this but the following code yields nothing?:

$output = $this->twig->getEnvironment()->render('template_file.twig', compact(...));
var_dump($output);
exit;
CarlosNegrao commented 2 years ago

I can't even get the output of var_dump($output), if it has one. $this->twig->getEnvironment()->render(...) still outputs the HTML straight to the browser, and nothing runs after it.

l0gicgate commented 2 years ago

Could you put a sample index.php file to reproduce the issue? I have a feeling you’re not shortcircuiting in the right place.

CarlosNegrao commented 2 years ago

I'm trying to, but it's not working out that well. I think I'll just give up on it and have it handled outside Slim (and possibly without Twig either). Thanks for the help anyway.