本文整理匯總了Python中tornado.template.Template方法的典型用法代碼示例。如果您正苦於以下問題:Python template.Template方法的具體用法?Python template.Template怎麽用?Python template.Template使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tornado.template
的用法示例。
在下文中一共展示了template.Template方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_break_continue
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_break_continue(self):
template = Template(
utf8(
"""\
{% for i in range(10) %}
{% if i == 2 %}
{% continue %}
{% end %}
{{ i }}
{% if i == 6 %}
{% break %}
{% end %}
{% end %}"""
)
)
result = template.generate()
# remove extraneous whitespace
result = b"".join(result.split())
self.assertEqual(result, b"013456")
示例2: test_error_line_number_module
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_error_line_number_module(self):
loader = None # type: typing.Optional[DictLoader]
def load_generate(path, **kwargs):
assert loader is not None
return loader.load(path).generate(**kwargs)
loader = DictLoader(
{"base.html": "{% module Template('sub.html') %}", "sub.html": "{{1/0}}"},
namespace={"_tt_modules": ObjectDict(Template=load_generate)},
)
try:
loader.load("base.html").generate()
self.fail("did not get expected exception")
except ZeroDivisionError:
exc_stack = traceback.format_exc()
self.assertTrue("# base.html:1" in exc_stack)
self.assertTrue("# sub.html:1" in exc_stack)
示例3: test_escaping
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_escaping(self):
self.assertRaises(ParseError, lambda: Template("{{"))
self.assertRaises(ParseError, lambda: Template("{%"))
self.assertEqual(Template("{{!").generate(), b"{{")
self.assertEqual(Template("{%!").generate(), b"{%")
self.assertEqual(Template("{{ 'expr' }} {{!jquery expr}}").generate(),
b"expr {{jquery expr}}")
示例4: test_unicode_literal_expression
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_unicode_literal_expression(self):
# Unicode literals should be usable in templates. Note that this
# test simulates unicode characters appearing directly in the
# template file (with utf8 encoding), i.e. \u escapes would not
# be used in the template file itself.
if str is unicode_type:
# python 3 needs a different version of this test since
# 2to3 doesn't run on template internals
template = Template(utf8(u('{{ "\u00e9" }}')))
else:
template = Template(utf8(u('{{ u"\u00e9" }}')))
self.assertEqual(template.generate(), utf8(u("\u00e9")))
示例5: test_apply
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_apply(self):
def upper(s):
return s.upper()
template = Template(utf8("{% apply upper %}foo{% end %}"))
self.assertEqual(template.generate(upper=upper), b"FOO")
示例6: test_unicode_apply
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_unicode_apply(self):
def upper(s):
return to_unicode(s).upper()
template = Template(utf8(u("{% apply upper %}foo \u00e9{% end %}")))
self.assertEqual(template.generate(upper=upper), utf8(u("FOO \u00c9")))
示例7: test_try
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_try(self):
template = Template(utf8("""{% try %}
try{% set y = 1/x %}
{% except %}-except
{% else %}-else
{% finally %}-finally
{% end %}"""))
self.assertEqual(template.generate(x=1), b"\ntry\n-else\n-finally\n")
self.assertEqual(template.generate(x=0), b"\ntry-except\n-finally\n")
示例8: test_simple
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_simple(self):
template = Template("Hello {{ name }}!")
self.assertEqual(template.generate(name="Ben"),
b"Hello Ben!")
示例9: test_bytes
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_bytes(self):
template = Template("Hello {{ name }}!")
self.assertEqual(template.generate(name=utf8("Ben")),
b"Hello Ben!")
示例10: test_expressions
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_expressions(self):
template = Template("2 + 2 = {{ 2 + 2 }}")
self.assertEqual(template.generate(), b"2 + 2 = 4")
示例11: test_comment
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_comment(self):
template = Template("Hello{# TODO i18n #} {{ name }}!")
self.assertEqual(template.generate(name=utf8("Ben")),
b"Hello Ben!")
示例12: test_bytes_apply
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_bytes_apply(self):
def upper(s):
return utf8(to_unicode(s).upper())
template = Template(utf8(u("{% apply upper %}foo \u00e9{% end %}")))
self.assertEqual(template.generate(upper=upper), utf8(u("FOO \u00c9")))
示例13: test_if
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_if(self):
template = Template(utf8("{% if x > 4 %}yes{% else %}no{% end %}"))
self.assertEqual(template.generate(x=5), b"yes")
self.assertEqual(template.generate(x=3), b"no")
示例14: test_comment_directive
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_comment_directive(self):
template = Template(utf8("{% comment blah blah %}foo"))
self.assertEqual(template.generate(), b"foo")
示例15: test_break_continue
# 需要導入模塊: from tornado import template [as 別名]
# 或者: from tornado.template import Template [as 別名]
def test_break_continue(self):
template = Template(utf8("""\
{% for i in range(10) %}
{% if i == 2 %}
{% continue %}
{% end %}
{{ i }}
{% if i == 6 %}
{% break %}
{% end %}
{% end %}"""))
result = template.generate()
# remove extraneous whitespace
result = b''.join(result.split())
self.assertEqual(result, b"013456")