关于 Mako 模块、Mako 的 TemplateLookup 函数和 Pyramid 的一些问题
我正在查看 Mako 的文档,找到了 Mako 的 TemplateLookup 函数: 使用模板查找。但是,我从未在 Pyramid 的文档中看到过这一点,因为我从未需要指定模块目录。我的问题是:
- 创建了哪些“模块”?这些是预编译的 .pyc 文件吗?
- 使用 TemplateLookup 与 Pyramid 的 render() 或 render_to_response() 会让模板更快吗?
- Pyramid 是否默认创建这些模块,但隐藏在用户看不到的地方?
- 从文档中可以看出,这些模块缓存在内存中。这与通过 Beaker 进行缓存有何不同?
由于我网站上的所有内容都是动态内容(基本上除了页脚),我想找出缓存模板或加速渲染的最佳方法,这看起来是一种加速渲染的简单方法,如果它确实如此的话。
I'm looking at Mako's documentation and I found a TemplateLookup function for Mako: Using TemplateLookup. However, I've never seen this in Pyramid's documentation since I've never had to specify a modules directory. My questions are:
- What "modules" are created? Are these like precompiled .pyc files?
- Will using TemplateLookup vs. Pyramid's render() or render_to_response() make templates faster?
- Does Pyramid create these modules by default, but hidden where the user can't see?
- From the documentation, it says that these modules are cached in memory. How is this different than caching via Beaker?
Since everything on my site is dynamic content (except for the footer, basically), I want to figure out the best way to cache my templates or speed up rendering, and this looks like a simple way to speed up rendering, if it even does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请在下面找到您问题的一些答案:
对于您拥有的每个模板,都会创建一个包含渲染模板所需代码的 Python 模块 (
.py
)。这只是模板的优化版本,可以轻松地从 python 执行。执行该模块时,还会创建.pyc
文件。要检查这一点,您可以做以下实验:假设
template.mako
存在,您将看到template.mako.py
和template.mako.pyc
已创建。查看pyramid.mako_templatating.MakoLookupRenderer.__call__,我发现用于在金字塔中渲染mako模板的方法已经使用了
TemplateLookup
对象,因此不会有有什么区别。我在
pyramid.mako_templatating.renderer_factory
中看到有一个名为mako.module_directory
的设置。这与其他类似的设置一起可用于控制 mako 库行为以创建模块文件。我看起来默认行为是不创建这些文件(mako.module_directory
默认情况下是None
),但您当然可以做任何您需要的事情。在
TemplateLookup
中可以看到一个名为cache_impl
的参数,默认情况下设置为beaker
,所以我猜没有任何区别。< /p>Please find below some answers to your questions:
For every template you have, a python module (
.py
) that contains the code needed to render the template is created. This is just an optimized version of the template that that can be executed easily from python. When that module is executed, the.pyc
file is also created. To check this you can do the following experiment:Assumming that
template.mako
exists, you'll see thattemplate.mako.py
andtemplate.mako.pyc
are created.Looking at
pyramid.mako_templating.MakoLookupRenderer.__call__
I see that the method used to render a mako template in pyramid already uses aTemplateLookup
object, so there won't be any difference.I see in
pyramid.mako_templating.renderer_factory
that there is a setting calledmako.module_directory
. This, together with other similar settings, can be used to controlmako
library behaviour to create module files. I looks like the default behaviour is not to create those files (mako.module_directory
isNone
by default), but you can certainly do whatever you need.In
TemplateLookup
is see a parameter calledcache_impl
that by default is set tobeaker
, so I guess there isn' any difference.请参阅 jcollado 对前三个问题的回答。对于问题4:
这些缓存两个不同的东西。 Beaker(或者您在
cache_impl
中设置的任何内容)缓存渲染的输出。如果设置module_directory
,从 mako 文件编译的 Python 模块将保存在此处。一张图片可能会更好地解释它:See jcollado's answer for the first three questions. For question 4:
These cache two different things. Beaker (or whatever you set in
cache_impl
) caches rendered output. If you set themodule_directory
, Python modules compiled from mako files are saved here. A picture might explain it better: