5

有没有办法将 html 代码从树枝模板放入 html 文件中。我的意思是,在这种情况下

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

有没有办法可以做到这一点?

$html->output($html_file);

它使用模板生成一个 html 文件。

我这样做是因为我有来自 wordpress 博客的新闻,我想在主页上显示最新消息。所以我想把我网站的新闻放在一个静态文件中,以避免在每个主页请求中生成它。

4

2 回答 2

7

如果你更仔细地检查文档,你会发现这一章,它说:

$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
于 2012-11-22T11:25:07.223 回答
4

要完成@VitalityZurian 的回答:

首先,生成您的标记:

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

然后,写入文件:

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

你可以使用 Symfony2 帮助方法来处理你的包中的位置:

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

并检索它:

$view = $this->get('kernel')->locateResource('@AcmeAnotherBundle/Resources/views/index.html.twig');
于 2016-02-06T14:09:03.343 回答