【发布时间】:2020-02-16 16:20:12
【问题描述】:
我在几个网站中使用 Smarty 3,我正在审查 twig 的工作原理。 与 Smarty 相比,Twig 有一件事我不明白,即如何管理同一模板的出现。
在 Smarty 中,首先使用模板名称和此模板的特定页面的缓存 ID 创建模板缓存
$template = $smarty->createTemplate('article.tpl', 'article|12');
这将创建一个缓存页面。
要在 Smarty 中呈现结果,您首先测试页面是否存在于缓存中,然后呈现它以访问数据库以检索数据并分配变量。
if (!$template->isCached()) {
// access to the database
// assign variables
}
然后页面被渲染
$tpl->display();
但在 Twig 中我只看到
$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, ['cache' =>
'/path/to/compilation_cache']);
$template = $twig->load('index.html');
echo $template->render(['the' => 'variables', 'go' => 'here']);
用什么相当于"!$template->isCached()"来避免每次访问数据库?
【问题讨论】: