首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在模块化PHP模板系统中使用Varnish ESI

问如何在模块化PHP模板系统中使用Varnish ESI
EN

Stack Overflow用户
提问于 2019-09-12 16:20:15
回答 1查看 98关注 0票数 1

我希望将varnish实现为一个数据密集型/以用户为中心的网站。如何在使用php包含html模板的系统中设置ESI?

我目前使用的是一个自定义的php模板系统(类似于MVC设计模式),它的工作原理如下:

生成页面请求-> php计算逻辑-> php包括html模板页面并填充变量输出->页面

我只在大多数html页面中使用过esi标签,包括php代码片段。

如以下代码所示:

代码语言:javascript
复制
<HTML>
<BODY>
The time is: <esi:include src="/php-includes/date.php"/>
at this very moment.
</BODY>
</HTML>

但是,是否可以反过来做呢?例如,在php页面中添加esi标签以包含html代码片段?

如下所示:

代码语言:javascript
复制
<?php
//logic here
$content = "this will be displayed on the content page"

include("templates/header.html.php"); //esi would go here since page is static content
include("templates/content.html.php"); //no esi here, since page is dynamic content
include("templates/footer.html.php"); //esi would go here since page is static content
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-12 22:23:07

您只需为您的特定MVC实现创建一种"ESI“渲染器,就像在/esi.php?template=foo中一样,然后在下面这样的东西中:

代码语言:javascript
复制
... whatever you need to boostrap your app in order to render a template ....
include("templates/$template.html.php");
.... exit so no header/fooder stuff is rendered, only template HTML of interest

当然不是那么简单,但简而言之,是一个类似的事情。

然后,我会在每个模板文件上放置一些逻辑,以便发出HTML (如果由esi.php呈现,或者如果ESI“功能”处于禁用状态)或仅发出<esi标记。

因此,例如,templates/header.html.php可以具有(伪代码):

代码语言:javascript
复制
if esi (detect by checking request_uri to be esi.php) then echo '<esi /esi.php?template=header?somevar-from-parent=...'; return
---existing code--
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57902569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档