~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to turbozpt/template.py

  • Committer: Robert Collins
  • Date: 2012-02-02 05:07:53 UTC
  • mfrom: (464.1.1 mod-wsgi)
  • Revision ID: robertc@robertcollins.net-20120202050753-2d8f8r6m24lglbz5
    - Add a script and documentation for running under mod_wsgi.
      (Stuart Colville, Toshio Kuratomi)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
By VladDrac@irc.freenode.net/#turbogears
3
 
+ Some small modifications
4
 
"""
5
 
 
6
 
from zope.pagetemplate import pagetemplatefile
7
 
import os.path
8
 
import sys
9
 
 
10
 
class Here(object):
11
 
    def __init__(self, base):
12
 
        self.base = base
13
 
 
14
 
    def __getattr__(self, name):
15
 
        # import pdb; pdb.set_trace()
16
 
        tpl = PageTemplate(os.path.join(self.base, name))
17
 
        return tpl
18
 
 
19
 
class PageTemplate(pagetemplatefile.PageTemplateFile):
20
 
    def __init__(self, name):
21
 
        base = os.path.dirname(sys._getframe(1).f_globals["__file__"])
22
 
        self.extra_context = {}
23
 
        self.name = name
24
 
        self.fullpath = os.path.join(base, self.name)
25
 
        self.base = os.path.dirname(self.fullpath)
26
 
        pagetemplatefile.PageTemplateFile.__init__(self, self.fullpath)
27
 
    
28
 
    def render(self, extra_dict=None):
29
 
        if extra_dict:
30
 
            context = self.pt_getContext()
31
 
            context.update(extra_dict)
32
 
        return self.pt_render(context)
33
 
    
34
 
    def add_context(self, d):
35
 
        self.extra_context.update(d)
36
 
        
37
 
    def pt_getContext(self, args=(), options={}, **ignored):
38
 
        rval = pagetemplatefile.PageTemplateFile.pt_getContext(self, args, options, **ignored)
39
 
        rval.update(options)
40
 
        rval.update(self.extra_context)
41
 
        rval.update({'here':Here(self.base), 'template':self})
42
 
        return rval