7

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118

2 Answers2

5

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.

jcollado
  • 39,419
  • 8
  • 102
  • 133
  • so i finally found documentation on pyramid about it (it always takes me a while for me to find anything i'm looking for on that site...). it seems like not setting mako.module_directory simply saves all the modules to memory by default. i find that confusing since Mako says modules saved to the directory are saved to memory as well. i guess i'm just going to use Pyramid's functions! – Jonathan Ong Jan 27 '12 at 10:53
  • Did you mean `MakoLookupTemplateRenderer`? – tshepang Jan 19 '13 at 22:00
  • @Tshepang The code might have changed since I wrote the answer. From what I see now, `MakoLookupTemplateRenderer` uses a `lookup` attribute that is created using `PkgResourceTemplateLookup` which uses `TemplateLookup` under the hood. – jcollado Jan 22 '13 at 09:18
3

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
Petr Viktorin
  • 65,510
  • 9
  • 81
  • 81