user3703456
user3703456

Reputation:

Symfony2 Save .twig output to web directory

I have a sitemap that is generated the same way as regular webpage(index, contacts, etc..). Now I want to just run cron once a day/week and save sitemap.xml to /web directory.

Is it possible to save .twig template output to /web directory of the website?

Thanks

PS: I'm planning to create symfony command to generate sitemap.xml to /web directory. But I'm not sure if it's gonna work.

Upvotes: 2

Views: 611

Answers (1)

Emii Khaos
Emii Khaos

Reputation: 10085

Yes. Simply create a command which generates your sitemap.xml. It doesn't matter if you render the sitemap with twig or php xml functions. You simply save it with php. So this may be in your command:

$sitemap = // however you collect the sites

$renderedSitemap = $this->getContainer()->get('templating')
    ->render('sitemap/sitemap.xml.twig', [
        'sitemap' => $sitemap,
    ]);

file_put_contents(
    dirname($this->getContainer()->getParameter('kernel.root_dir')).'/web/sitemap.xml',
    $renderedSitemap
);

That's all. Your command must extend ContainerAwareCommand to have access to the container. And the constructed path expects the standard symfony structure. If you have changed that, you must adapt the path.

Upvotes: 1

Related Questions