~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to turbozpt/template.py

  • Committer: Michael Hudson
  • Date: 2008-06-23 02:07:08 UTC
  • mfrom: (166.1.3 config-vhost)
  • Revision ID: michael.hudson@canonical.com-20080623020708-eiwjeg8bhjxxnyvj
more compatible handling of server.webpath from loggerhead.conf files

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, options):
12
 
        self.base = base
13
 
        self.options = options
14
 
 
15
 
    def __getitem__(self, name):
16
 
        tpl = PageTemplate(os.path.join(self.base, name))
17
 
        tpl.add_context(self.options)
18
 
        return tpl
19
 
 
20
 
class PageTemplate(pagetemplatefile.PageTemplateFile):
21
 
    def __init__(self, name):
22
 
        base = os.path.dirname(sys._getframe(1).f_globals["__file__"])
23
 
        self.extra_context = {}
24
 
        self.name = name
25
 
        self.fullpath = os.path.join(base, self.name)
26
 
        self.base = os.path.dirname(self.fullpath)
27
 
        pagetemplatefile.PageTemplateFile.__init__(self, self.fullpath)
28
 
    
29
 
    def render(self, extra_dict=None):
30
 
        if extra_dict:
31
 
            context = self.pt_getContext()
32
 
            context.update(extra_dict)
33
 
        return self.pt_render(context)
34
 
    
35
 
    def add_context(self, d):
36
 
        self.extra_context.update(d)
37
 
        
38
 
    def pt_getContext(self, args=(), options={}, **ignored):
39
 
        rval = pagetemplatefile.PageTemplateFile.pt_getContext(self, args, options, **ignored)
40
 
        rval.update(options)
41
 
        rval.update(self.extra_context)
42
 
        rval.update({'here':Here(self.base, options), 'template':self})
43
 
        return rval