【问题标题】:From Smarty 3 to Twig 2, how occurences of a template are managed?从 Smarty 3 到 Twig 2,如何管理模板的出现?
【发布时间】: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()"来避免每次访问数据库?

【问题讨论】:

    标签: caching twig smarty


    【解决方案1】:

    没有。 Twig 不会自行缓存呈现的 HTML 输出。它只是将模板编译的结果缓存到 PHP 代码中,每次渲染模板时仍然会执行。

    您将需要在您的应用程序前面使用反向代理,或某种数据库缓存层以避免重复调用您的应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-25
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 2014-02-06
      • 2013-06-08
      • 2020-04-21
      相关资源
      最近更新 更多