本文整理汇总了Python中template.Template.context方法的典型用法代码示例。如果您正苦于以下问题:Python Template.context方法的具体用法?Python Template.context怎么用?Python Template.context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类template.Template
的用法示例。
在下文中一共展示了Template.context方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testContext
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import context [as 别名]
def testContext(self):
tt = Template({ 'INCLUDE_PATH': 'test/src:test/lib',
'TRIM': True,
'POST_CHOMP': 1 })
ttpython = Template({ 'INCLUDE_PATH': 'test/src:test/lib',
'TRIM': True,
'POST_CHOMP': 1,
'EVAL_PYTHON': True })
ctx = tt.service().context()
self.failUnless(ctx)
self.assertEquals(ctx, tt.context())
self.failUnless(ctx.trim())
self.failUnless(not ctx.eval_python())
ctx = ttpython.service().context()
self.failUnless(ctx)
self.failUnless(ctx.trim())
self.failUnless(ctx.eval_python())
# template()
# Test that we can fetch a template via template()
tmpl = ctx.template('header')
self.failUnless(tmpl)
self.failUnless(isinstance(tmpl, Document))
# Test that non-existence of a template is reported
error = None
try:
tmpl = ctx.template('no_such_template')
except Exception, e:
error = e
示例2: testView
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import context [as 别名]
def testView(self):
vars = {"foo": Foo(pi=3.14, e=2.718), "blessed_list": MyList("Hello", "World")}
t = Template()
context = t.context()
view = context.view()
self.assert_(view)
view = context.view({"prefix": "my"})
self.assert_(view)
self.assertEquals("my", view.prefix())
self.Expect(DATA, None, vars)
示例3: testFilter
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import context [as 别名]
def testFilter(self):
dir = 'test/tmp'
file = 'xyz'
a, b, c, d = 'alpha', 'bravo', 'charlie', 'delta'
params = { 'a': a,
'b': b,
'c': c,
'd': d,
'list': [ a, b, c, d ],
'text': 'The cat sat on the mat',
'outfile': file,
'stderr': lambda *_: sys.stderr.getvalue(),
}
filters = { 'nonfilt': 'nonsense',
'microjive': microjive,
'microsloth': microsloth,
'censor': censor_factory,
'badfact': dynamic_filter(lambda *_: 'nonsense'),
'badfilt': 'rubbish',
'barfilt': barf_up }
config1 = { 'INTERPOLATE': 1,
'POST_CHOMP': 1,
'FILTERS': filters }
config2 = { 'EVAL_PYTHON': 1,
'FILTERS': filters,
'OUTPUT_PATH': dir,
'BARVAL': 'some random value' }
path = os.path.join(dir, file)
if os.path.exists(path):
os.remove(path)
tt1 = Template(config1)
tt2 = Template(config2)
tt2.context().define_filter('another', another, True)
self.Expect(DATA, (('default', tt1), ('evalpython', tt2)), params)
self.failUnless(os.path.exists(path))
os.remove(path)