Closed CarlosNegrao closed 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);
}
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', []);
$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.
I just tried it, and got the error "Call to undefined method Twig\Environment::fetch()".
I meant $this->twig->getEnvironment()->render(...)
my bad
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.
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;
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.
Could you put a sample index.php file to reproduce the issue? I have a feeling you’re not shortcircuiting in the right place.
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.
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?