我如何在 smarty 中使用 ETag?
我使用 smarty 作为模板引擎,YSlow(Firefox 插件)报告我的 Etag 标志是“F”,但我使用 smarty。 我如何将 Etag 与 smarty 一起使用,而我的主要 tpl 是 Framework.tpl 并将所有其他模板分配给它
I use smarty as my template engine and YSlow ( Firefox addon ) report me that my Etag flag is "F" But i use smarty.
how could i use Etag with smarty and my main tpl is framework.tpl and all other templates assign to it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确定您的页面上确实需要 ETag(这与仅仅为了通过某种任意测试而实现它们不同),我对这个(较旧的)脚本有很好的经验:http://simonwillison.net/2003/Apr/23/conditionalGet/
请注意这并不是 Smarty 特有的,因为 Smarty 处理页面模板;整个“条件请求”业务(使用 ETag 和 Last-Modified 等)发生在 HTTP headers 中,因此上面的代码只是普通的 PHP。
您需要确定资源的上次修改时间(这可能与文件修改不同 - 例如数据库的相关部分上次更改是什么时候?),并将其传递给
doConditionalGet($timestamp);然后,它会发送 304 并终止,或者返回给您(这意味着您需要执行完整的页面处理,就好像没有任何条件请求一样)。
If you decide that you actually need ETags on your pages (which is different from implementing them just to pass a somewhat arbitrary test), I've had good experience with this (older) script: http://simonwillison.net/2003/Apr/23/conditionalGet/
Note that this is not really Smarty-specific, as Smarty deals with page templates; this whole "conditional request" business (with ETag and Last-Modified and whatnot) happens in HTTP headers, so the code above is just plain PHP.
You need to determine the last modification time of your resource (which may be different from file modification - e.g. when was the last time the relevant part of the database changed?), and pass it to
doConditionalGet($timestamp)
; it will then either send a 304 and terminate, or return back to you (which means you need to do the full page processing, as if there wasn't any conditional request).只需在 .htaccess 文件中添加指令
即可禁用 ETag 标头字段。 YSlow不会再抱怨了...:)
Simply but the directive
in your .htaccess file to disable the ETag header field. YSlow will no longer complain... :)
您可以在 Smarty 中使用输出过滤器。下面是一些用于 ETagging 的现成代码:
将其放在
$smarty->display(...);
之前。请注意,使用此代码,服务器仍然计算输出,但如果客户端已经有相同的答案,服务器将不会再次发送它,因此它只是一个网络流量节省器。如果你想节省服务器中的计算能力,你必须做一些其他的事情(缓存),但你可以将它与ETagging结合起来。
You can use an output filter in Smarty. Below some ready-to-use code for ETagging:
Put that somewhere before
$smarty->display(...);
.Note, that with this code the server still computes the output, but if the client has already the identical answer, the server won't send it again, so it's just a network traffic saver. If you want to save computing power in the server, you have to do something else (caching), but you can combine it with ETagging.