Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some questions regarding Mako modules, Mako's TemplateLookup function, and Pyramid

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:

  1. What "modules" are created? Are these like precompiled .pyc files?
  2. Will using TemplateLookup vs. Pyramid's render() or render_to_response() make templates faster?
  3. Does Pyramid create these modules by default, but hidden where the user can't see?
  4. 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.

like image 781
Jonathan Ong Avatar asked Jan 27 '12 03:01

Jonathan Ong


2 Answers

Please find below some answers to your questions:

  1. 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.

  2. 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.

  3. 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.

  4. In TemplateLookup is see a parameter called cache_impl that by default is set to beaker, so I guess there isn' any difference.

like image 131
jcollado Avatar answered Sep 21 '22 17:09

jcollado


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
like image 20
Petr Viktorin Avatar answered Sep 23 '22 17:09

Petr Viktorin