-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocks.py
78 lines (64 loc) · 2.04 KB
/
blocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from google.appengine.ext.webapp import template
from lib.registry import Registry
import genshi
from genshi.template import TemplateLoader
from genshi import Markup
def render_template(template_name, template_vals={}):
loader = TemplateLoader('theme/frontend')
template = loader.load(template_name)
template_vals['render']=render_template
template_vals['Registry']=Registry
stream = template.generate(**template_vals)
rendered = stream.render()
return Markup(rendered.decode('utf-8'))
def render_django_template(template_name, template_vals=None):
path = os.path.join(os.path.dirname(__file__), 'theme/frontend', template_name + '.html')
rendered = template.render(path, template_vals)
return rendered
class Block(object):
child_blocks = {}
template_file = ''
template_vals = {}
name = ''
template_engine = 'genshi'
def __init__(self, template_name=False, name=False):
self.template_name = template_name
self.name = name
def render(self):
html = ''
if self.blocks:
for block in self.blocks.values():
html += block.render()
else:
html = {
'genshi': render_template,
'django': render_django_template
}[self.template_engine](self.template_file, self.template_vals)
return html
class Layout(object):
blocks = {}
root = Block('layout', 'root')
def render(self):
return self.root.render()
@classmethod
def addBlock(cls,template_file=None,name=None, BlockObj=None):
if BlockObj:
block = BlockObj()
else:
block = Block()
if name:
block.name = name
else:
block.name = cls._generateName()
block.template_file = template_file
cls.blocks[block.name] = block
return block
@classmethod
def _generateName(cls):
return "anonymous_%i" % len(cls.blocks)
class Head(Block):
title = ''
css_links = []
class Header(Block):
title = ''