本文整理汇总了Python中werkzeug.templates.Template类的典型用法代码示例。如果您正苦于以下问题:Python Template类的具体用法?Python Template怎么用?Python Template使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Template类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_multidict
def test_multidict():
"""Template multidict behavior"""
t = Template('$a|$b')
assert t.render(MultiDict(dict(
a=[1, 2],
b=2
))) == '1|2'
示例2: test_code
def test_code():
"""Template code block"""
t = Template('''<%py
a = 'A'
b = 'B'
%>$a$b''')
assert t.render() == 'AB'
示例3: test_if
def test_if():
"""Template if condition"""
t = Template('<% if idx == 1 %>ONE<% elif idx == 2 %>TWO<% elif '
'idx == 3 %>THREE<% else %>OMGWTF<% endif %>')
assert t.render(idx=0) == 'OMGWTF'
assert t.render(idx=1) == 'ONE'
assert t.render(idx=2) == 'TWO'
assert t.render(idx=3) == 'THREE'
示例4: render_template
def render_template(self, template=None):
if template is None:
template = self.__class__.identifier + '.html'
context = dict(self.__dict__)
context.update(url_for=self.url_for, self=self)
body_tmpl = Template.from_file(path.join(templates, template))
layout_tmpl = Template.from_file(path.join(templates, 'layout.html'))
context['body'] = body_tmpl.render(context)
return layout_tmpl.render(context)
示例5: get_template
def get_template(self, name):
"""Get a template from a given name."""
filename = path.join(self.search_path, *[p for p in name.split('/')
if p and p[0] != '.'])
if not path.exists(filename):
raise TemplateNotFound(name)
return Template.from_file(filename, self.encoding)
示例6: generate
def generate(self, rsp, **kwargs):
global settings
kwargs['cfg_line'] = lambda s, d=None: s + ' = ' + repr(settings.get(s, d))
for filename in ('launch.wsgi', 'app.py', 'settings.py', 'manage.py'):
with open(in_cwd(filename), 'w') as f:
# Template strips off trailing whitespace...
f.write(Template.from_file(in_skeld(filename + '.tmpl')).render(**kwargs) + '\n')
os.chmod(in_cwd('manage.py'), 0755)
示例7: test_interpolation
def test_interpolation():
"""Template variable interpolation"""
t = Template('\n'.join([
'$string',
'${", ".join(string.upper().split(" AND "))}',
'$string.replace("foo", "bar").title()',
'${string}s',
'${1, 2, 3}',
'$string[0:3][::-1]'
]))
assert t.render(string='foo and blah').splitlines() == [
'foo and blah',
'FOO, BLAH',
'Bar And Blah',
'foo and blahs',
'(1, 2, 3)',
'oof'
]
示例8: test_from_file_with_filename
def test_from_file_with_filename():
"""Template from_file where file parameter is a filename"""
fd, filename = tempfile.mkstemp()
try:
os.write(fd, "Hello ${you}!")
finally:
os.close(fd)
try:
t = Template.from_file(filename)
assert t.render(you="World") == "Hello World!"
finally:
os.unlink(filename)
示例9: test_break
def test_break():
"""Template brake statement"""
t = Template('<% for i in xrange(5) %><%py break %>$i<% endfor %>')
assert t.render() == ''
示例10: test_nl_trim
def test_nl_trim():
"""Template newline trimming"""
t = Template('<% if 1 %>1<% endif %>\n2')
assert t.render() == '12'
示例11: test_unicode
def test_unicode():
"""Template unicode modes"""
t = Template(u'öäü$szlig')
assert t.render(szlig='ß') == u'öäüß'
t = Template(u'öäü$szlig', unicode_mode=False, charset='iso-8859-15')
assert t.render(szlig='\xdf') == '\xf6\xe4\xfc\xdf'
示例12: test_undefined
def test_undefined():
"""Template undefined behavior"""
t = Template('<% for item in seq %>$item<% endfor %>$missing')
assert t.render() == ''
示例13: test_from_file_with_fileobject
def test_from_file_with_fileobject():
"""Template from_file where file parameter is a file object"""
t = Template.from_file(sio.StringIO("Hello ${you}!"))
assert t.render(you="World") == "Hello World!"
示例14: test_print
def test_print():
"""Template print helper"""
t = Template('1 <%py print "2", %>3')
t.render() == '1 2 3'
示例15: test_for
def test_for():
"""Template for loop"""
t = Template('<% for i in range(10) %>[$i]<% endfor %>')
assert t.render() == ''.join(['[%s]' % i for i in xrange(10)])