Generate the HTML template equivalent of a twig file?
Asked Answered
S

2

1

I'd like to know if it's possible to have {filename}.html generated for every {filename}.html.twig? I know one solution is to copy and paste the source code from the browser, but is there a more efficient way of doing this?

Thank you in advance.

Sponge answered 28/7, 2016 at 19:49 Comment(2)
You want to cache the twig output?Nereidanereids
Yes, you worded it better than I could @NereidanereidsSponge
S
1

Such solution, I think, will satisfy your needs.

curl www.yoururl.com > /path/to/yourfolder/file.html

Have a look below story, if you interested in.

Story

A few months ago, i was working on a yii2 project. The index page was loading in 3 seconds because of response from external services. Cloudflare and other caching scenarios weren't succeeded - I came up with 2.5 seconds best.

After 2-3 days of thinking period, I found a nasty solution :) Created a cache-less, fresh version of homepage and wrote a crone job, to fetch the contents of that page and write into index.html file inside public (in yii2 web) folder in every 2 minutes.

Other pages were ok - (<0.2s), so didn't wrote anything for them.

All I've done, was

curl www.example.com > /path/to/web/index.html

Sienna answered 28/7, 2016 at 20:10 Comment(0)
N
1

If you want to create these file automatically with each render, then I suggest to overwrite the default render method of the Twig_Environment and create an instance of your custom class to init Twig

class MyTwigEnvironment extends \Twig_Environment {
    public function render($name, array $context = array())
    {
        $html = $this->loadTemplate($name)->render($context, $site_id);
        file_put_contents('path/to/cache/folder'.$name.'.twig', $html);
        chmod('path/to/cache/folder'.$name.'.twig', 0664);
        return $html;
    }   
 }

$twig = new MyTwigEnvironment($loader, $options);
echo $twig->render('some_template.html');
Nereidanereids answered 28/7, 2016 at 20:0 Comment(2)
Where would I create the instance? (Very new to twig) @NereidanereidsSponge
Are you using twig standalone or with symfony, silex, ... ?Nereidanereids
S
1

Such solution, I think, will satisfy your needs.

curl www.yoururl.com > /path/to/yourfolder/file.html

Have a look below story, if you interested in.

Story

A few months ago, i was working on a yii2 project. The index page was loading in 3 seconds because of response from external services. Cloudflare and other caching scenarios weren't succeeded - I came up with 2.5 seconds best.

After 2-3 days of thinking period, I found a nasty solution :) Created a cache-less, fresh version of homepage and wrote a crone job, to fetch the contents of that page and write into index.html file inside public (in yii2 web) folder in every 2 minutes.

Other pages were ok - (<0.2s), so didn't wrote anything for them.

All I've done, was

curl www.example.com > /path/to/web/index.html

Sienna answered 28/7, 2016 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.