-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheetahTest.py
144 lines (97 loc) · 2.31 KB
/
cheetahTest.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# coding: utf-8
__author__ = 'Administrator'
from Cheetah.Template import Template
import os
templateDef = """
<HTML>
<HEAD><TITLE>$title</TITLE></HEAD>
<BODY>
$contents
## this is a single-line Cheetah comment and won't appear in the output
#* This is a multi-line comment and won't appear in the output
blah, blah, blah
*#
</BODY>
</HTML>"""
# 0
def test0():
nameSpace = {'title': 'Hello World Example', 'contents': 'Hello World!'}
t = Template(templateDef, searchList=[nameSpace])
print(t)
# 1
def test1():
t = Template(templateDef)
t.title = 'MyTitle'
t.contents = 'MyContents'
print(t)
t.title = 'my_title'
t.contents = 'my_contents'
print(t)
# 2
class TemplateDef(Template):
title = 'class_title'
contents = 'class_contents'
def test2():
obj = TemplateDef(templateDef)
print(obj)
# 3
class PrecompiledTemplate(Template):
def __init__(self):
super(self.__class__, self).__init__()
def test3():
t = PrecompiledTemplate()
t.title = 'precompiled_title'
t.content = 'precompiled_contents'
print(t)
# 4
def test4():
t = Template('just simple')
print(t)
# 5
class SearchObject(object):
name = 'Lucy'
def __init__(self):
self.name = 'Tommy'
def test5():
templateStr = 'You say:\n\tHello, $name!'
fileName = 'F:/_python/HelloWorld.tmpl'
fileName1 = 'F:/_python/HelloWorld.txt'
t = Template(templateStr)
t.name = 'Tom' # 没有name会异常
print(t)
t = Template(file = fileName)
t.name = 'Jerray'
print(t)
f = open(fileName)
t = Template(file = f)
t.name = 'Jack'
print(t)
searchList = ({'name' : 'abc'}, SearchObject())
# 用class打印Lucy,用object打印Tommy,顺序从前往后
t = Template(templateStr, searchList = searchList)
print(t)
t = Template(file = fileName1, searchList = searchList)
print(t)
f.seek(0)
t = Template(file = f, searchList = searchList)
print(t)
f.close()
# 6
def test6():
# templateStr = '''
# #set $namespace0 = '[namespace0]'
# #set $subPlaceHolder = '[sub]'
# #set $namespace1 = '[namespace1]'
# $namespace0.name['attr']('abc', $subPlaceHolder, 1)
# ${namespace1.name['attr']('abc', $subPlaceHolder, 1)}
# '''
templateStr = ''''''
t = Template(templateStr)
print(t)
# if __name__ == '__main__':
# test1()
# test2()
# test3()
# test4()
# test5()
test6()