Some questions regarding Mako modules, Mako TemplateLookup and Pyramid functions

I am looking at the Mako documentation and I found the TemplateLookup function for Mako: Using TemplateLookup . However, I never saw this in the Pyramid documentation, since I never had to specify a module directory. My questions:

  • What "modules" are created? Is it like precompiled .pyc files?
  • Will use TemplateLookup vs. Pyramid render () or render_to_response () make templates faster?
  • Does Pyramid create these modules by default, but hide where the user cannot see?
  • The documentation says that these modules are cached in memory. How is this different from caching through Beaker?

Since everything on my site is dynamic content (with the exception of the footer, basically), I want to find a better way to cache my templates or speed up the rendering, and this looks like an easy way to speed up the rendering, even if it does.

+7
source share
2 answers

Below you will find the answers to your questions:

  • For each template, you create a python module ( .py ) that contains the code needed to render the template. This is just an optimized version of the template that can easily be executed from python. When this module is executed, a .pyc file is created. To test this, you can do the following experiment:

     from mako.template import Template Template(filename='template.mako', module_directory='.') 

    Assuming template.mako exists, you will see that template.mako.py and template.mako.pyc are being created.

  • Looking at pyramid.mako_templating.MakoLookupRenderer.__call__ , I see that the method used to render the mako template in the pyramid already uses the TemplateLookup object, so there will be no difference.

  • In pyramid.mako_templating.renderer_factory I see that there is a parameter called mako.module_directory . This, together with other similar settings, can be used to control the mako library behavior for creating module files. It seems like the default behavior is not creating these files ( mako.module_directory is None by default), but you can certainly do whatever you need.

  • TemplateLookup displays the cache_impl parameter, which is set to beaker by default, so I think there is no difference.

+5
source

See jcollado answer for the first three questions. To question 4:

The documentation says that these modules are cached in memory. How is this different from caching through Beaker?

These cache two different things. A glass (or everything that you set in cache_impl ) gives the resulting cache. If you set module_directory , Python modules compiled from mako files will be saved here. An image may explain this better:

  context variables | v Template() render() .mako file -------------> python module (.py, .pyc) -----------> output : : | | cached in cached module_directory via Beaker 
+3
source

All Articles