How let Twig write the HTML output to a file, instead to a browser?
Asked Answered
S

1

6

I have a Twig template (.html file) that I want to render (replace the tokens with values from my PHP file). Instead of sending the rendered result to a browser, however, I want to write it to a new .html file. I would imagine the solution would also be similar for a Django template.

How can I do this in PHP?

Smallish answered 2/10, 2015 at 7:29 Comment(2)
Everything you need is here link to official doc. Else, share some code you tried...Polymerous
@YenneInfo I have read the documentation carefully. It's mainly geared into how configure the output towards the browser. Not about where I could 'catch' the HTML-output and write it to a file instead.Smallish
G
8

You can get the rendered output assigned to a variable and then do with it what you please.

$bob = $this->render('MyBundle:index.html.twig',[
            'foo' => $foo,
            'token' => $token
        ]);
dump($bob->getContent());exit(0);
Gignac answered 2/10, 2015 at 7:57 Comment(3)
I never thought of that.. thanks, it seems so simple now.Smallish
@FabienSnauwaert it is a symfony function. symfony.com/doc/current/components/var_dumper.htmlGignac
if you don't want the headers to be included in the file, use renderView instead of renderConchita

© 2022 - 2024 — McMap. All rights reserved.