web.web.contrib.template

Package web :: Package web :: Package contrib :: Module template |
[hide private]
[frames] | no frames]

# Source Code for Module web.web.contrib.template

      1  """
      2  Interface to various templating engines.
      3  """
      4  import os.path
      5
      6  __all__ = [
      7      "render_cheetah", "render_genshi", "render_mako",
      8      "cache",
      9  ]
     10
     11 -class render_cheetah:
     12      """Rendering interface to Cheetah Templates.
     13
     14      Example:
     15
     16          render = render_cheetah('templates')
     17          render.hello(name="cheetah")
     18      """
     19 -    def __init__(self, path):
     20          # give error if Chetah is not installed
     21          from Cheetah.Template import Template
     22          self.path = path
     23
     24 -    def __getattr__(self, name):
     25          from Cheetah.Template import Template
     26          path = os.path.join(self.path, name + ".html")
     27
     28          def template(**kw):
     29              t = Template(file=path, searchList=[kw])
     30              return t.respond()
     31
     32          return template
     33
     34 -class render_genshi:
     35      """Rendering interface genshi templates.
     36      Example:
     37
     38      for xml/html templates.
     39
     40          render = render_genshi(['templates/'])
     41          render.hello(name='genshi')
     42
     43      For text templates:
     44
     45          render = render_genshi(['templates/'], type='text')
     46          render.hello(name='genshi')
     47      """
     48
     49 -    def __init__(self, *a, **kwargs):
     50          from genshi.template import TemplateLoader
     51
     52          self._type = kwargs.pop('type', None)
     53          self._loader = TemplateLoader(*a, **kwargs)
     54
     55 -    def __getattr__(self, name):
     56          # Assuming all templates are html
     57          path = name + ".html"
     58
     59          if self._type == "text":
     60              from genshi.template import TextTemplate
     61              cls = TextTemplate
     62              type = "text"
     63          else:
     64              cls = None
     65              type = None
     66
     67          t = self._loader.load(path, cls=cls)
     68          def template(**kw):
     69              stream = t.generate(**kw)
     70              if type:
     71                  return stream.render(type)
     72              else:
     73                  return stream.render()
     74          return template
     75
     76 -class render_jinja:
     77      """Rendering interface to Jinja2 Templates
     78
     79      Example:
     80
     81          render= render_jinja('templates')
     82          render.hello(name='jinja2')
     83      """
     84 -    def __init__(self, *a, **kwargs):
     85          extensions = kwargs.pop('extensions', [])
     86          globals = kwargs.pop('globals', {})
     87
     88          from jinja2 import Environment,FileSystemLoader
     89          self._lookup = Environment(loader=FileSystemLoader(*a, **kwargs), extensions=extensions)
     90          self._lookup.globals.update(globals)
     91
     92 -    def __getattr__(self, name):
     93          # Assuming all templates end with .html
     94          path = name + '.html'
     95          t = self._lookup.get_template(path)
     96          return t.render
     97
     98 -class render_mako:
     99      """Rendering interface to Mako Templates.
    100
    101      Example:
    102
    103          render = render_mako(directories=['templates'])
    104          render.hello(name="mako")
    105      """
    106 -    def __init__(self, *a, **kwargs):
    107          from mako.lookup import TemplateLookup
    108          self._lookup = TemplateLookup(*a, **kwargs)
    109
    110 -    def __getattr__(self, name):
    111          # Assuming all templates are html
    112          path = name + ".html"
    113          t = self._lookup.get_template(path)
    114          return t.render
    115
    116 -class cache:
    117      """Cache for any rendering interface.
    118
    119      Example:
    120
    121          render = cache(render_cheetah("templates/"))
    122          render.hello(name='cache')
    123      """
    124 -    def __init__(self, render):
    125          self._render = render
    126          self._cache = {}
    127
    128 -    def __getattr__(self, name):
    129          if name not in self._cache:
    130              self._cache[name] = getattr(self._render, name)
    131          return self._cache[name]
    132