~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to turbozpt/template.py

  • Committer: John Arbash Meinel
  • Date: 2011-02-09 23:36:53 UTC
  • mto: This revision was merged to the branch mainline in revision 426.
  • Revision ID: john@arbash-meinel.com-20110209233653-o3o1ywpqamraq7o9
Hook everything up all the way to the main 'run' command.

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