~loggerhead-team/loggerhead/trunk-rich

128.6.1 by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code)
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):
128.6.5 by Michael Hudson
start some reuse potential in the page templates.
11
    def __init__(self, base, options):
128.6.1 by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code)
12
        self.base = base
128.6.5 by Michael Hudson
start some reuse potential in the page templates.
13
        self.options = options
128.6.1 by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code)
14
128.6.7 by Michael Hudson
oops, forgot some links
15
    def __getitem__(self, name):
16
        tpl = PageTemplate(os.path.join(self.base, name))
17
        tpl.add_context(self.options)
128.6.1 by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code)
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)
128.6.5 by Michael Hudson
start some reuse potential in the page templates.
42
        rval.update({'here':Here(self.base, options), 'template':self})
128.6.1 by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code)
43
        return rval