5

Is there a way to put the html code from a twig template into a html file. I mean, in this case

$html = $this->render('SiteBundle:Generated:home_news.html.twig', array('news' => $news))->getContent();

Is there a way where I can do this?

$html->output($html_file);

And it gerenates a html file with the template.

I'm doing this because I have news from a wordpress blog and I want to show the latest news in the homepage. So I would want to put the news of my site in a static file to avoid to generate it with each home request.

2 Answers 2

7

If you check documentation more carefully, you'd find this chapter, which says:

$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response but sorry, I guess you haven't understand me. I dont want to show the output of the twig template. I'd want to keep this content into a html file, which I'll use in other places.
So file_put_contents() your $content - no? :)
yes, but I am asking about a way to do it with twig or with S2
There's no way to render directly to a file. Getting the render output as a string and using file_put_contents() on it is perfectly fine.
5

To complete the @VitalityZurian answer:

First, generate your markup :

$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));

Then, write in file :

$file = file_put_contents($filename, $content);

You can use Symfony2 help methods to deal with locations in your bundles:

$bundleResourcesDir = $this->get('kernel')->locateResource('@AcmeAnotherBundle/Resources/views');
file_put_contents($bundleResourcesDir.'/index.html.twig`

And retrieve it:

$view = $this->get('kernel')->locateResource('@AcmeAnotherBundle/Resources/views/index.html.twig');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.