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:
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.
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:
from mako.template import Template
Template(filename='template.mako', module_directory='.')
Assumming that template.mako
exists, you'll see that template.mako.py
and template.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 a TemplateLookup
object, so there won't be any difference.
I see in pyramid.mako_templating.renderer_factory
that there is a setting called mako.module_directory
. This, together with other similar settings, can be used to control mako
library behaviour to create module files. I looks like the default behaviour is not to create those files (mako.module_directory
is None
by default), but you can certainly do whatever you need.
In TemplateLookup
is see a parameter called cache_impl
that by default is set to beaker
, so I guess there isn' any difference.
See jcollado's answer for the first three questions. For question 4:
From the documentation, it says that these modules are cached in memory. How is this different than caching via Beaker?
These cache two different things. Beaker (or whatever you set in cache_impl
) caches rendered output. If you set the module_directory
, Python modules compiled from mako files are saved here. A picture might explain it better:
context variables
|
v
Template() render()
.mako file -------------> python module (.py, .pyc) -----------> output
: :
| |
cached in cached
module_directory via Beaker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With