本文整理汇总了Python中tornado.template.DictLoader方法的典型用法代码示例。如果您正苦于以下问题:Python template.DictLoader方法的具体用法?Python template.DictLoader怎么用?Python template.DictLoader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.template
的用法示例。
在下文中一共展示了template.DictLoader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_custom_escape
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_custom_escape(self):
loader = DictLoader({"foo.py":
"{% autoescape py_escape %}s = {{ name }}\n"})
def py_escape(s):
self.assertEqual(type(s), bytes)
return repr(native_str(s))
def render(template, name):
return loader.load(template).generate(py_escape=py_escape,
name=name)
self.assertEqual(render("foo.py", "<html>"),
b"s = '<html>'\n")
self.assertEqual(render("foo.py", "';sys.exit()"),
b"""s = "';sys.exit()"\n""")
self.assertEqual(render("foo.py", ["not a string"]),
b"""s = "['not a string']"\n""")
示例2: test_whitespace_by_filename
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_whitespace_by_filename(self):
# Default whitespace handling depends on the template filename.
loader = DictLoader({
"foo.html": " \n\t\n asdf\t ",
"bar.js": " \n\n\n\t qwer ",
"baz.txt": "\t zxcv\n\n",
"include.html": " {% include baz.txt %} \n ",
"include.txt": "\t\t{% include foo.html %} ",
})
# HTML and JS files have whitespace compressed by default.
self.assertEqual(loader.load("foo.html").generate(),
b"\nasdf ")
self.assertEqual(loader.load("bar.js").generate(),
b"\nqwer ")
# TXT files do not.
self.assertEqual(loader.load("baz.txt").generate(),
b"\t zxcv\n\n")
# Each file maintains its own status even when included in
# a file of the other type.
self.assertEqual(loader.load("include.html").generate(),
b" \t zxcv\n\n\n")
self.assertEqual(loader.load("include.txt").generate(),
b"\t\t\nasdf ")
示例3: test_whitespace_by_loader
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_whitespace_by_loader(self):
templates = {
"foo.html": "\t\tfoo\n\n",
"bar.txt": "\t\tbar\n\n",
}
loader = DictLoader(templates, whitespace='all')
self.assertEqual(loader.load("foo.html").generate(), b"\t\tfoo\n\n")
self.assertEqual(loader.load("bar.txt").generate(), b"\t\tbar\n\n")
loader = DictLoader(templates, whitespace='single')
self.assertEqual(loader.load("foo.html").generate(), b" foo\n")
self.assertEqual(loader.load("bar.txt").generate(), b" bar\n")
loader = DictLoader(templates, whitespace='oneline')
self.assertEqual(loader.load("foo.html").generate(), b" foo ")
self.assertEqual(loader.load("bar.txt").generate(), b" bar ")
示例4: get_app_kwargs
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def get_app_kwargs(self):
def my_ui_method(handler, x):
return "In my_ui_method(%s) with handler value %s." % (
x, handler.value())
class MyModule(UIModule):
def render(self, x):
return "In MyModule(%s) with handler value %s." % (
x, self.handler.value())
loader = DictLoader({
'foo.html': '{{ my_ui_method(42) }} {% module MyModule(123) %}',
})
return dict(template_loader=loader,
ui_methods={'my_ui_method': my_ui_method},
ui_modules={'MyModule': MyModule})
示例5: get_app_kwargs
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def get_app_kwargs(self):
loader = DictLoader({
"linkify.html": "{% module linkify(message) %}",
"page.html": """\
<html><head></head><body>
{% for e in entries %}
{% module Template("entry.html", entry=e) %}
{% end %}
</body></html>""",
"entry.html": """\
{{ set_resources(embedded_css=".entry { margin-bottom: 1em; }", embedded_javascript="js_embed()", css_files=["/base.css", "/foo.css"], javascript_files="/common.js", html_head="<meta>", html_body='<script src="/analytics.js"/>') }}
<div class="entry">...</div>""",
})
return dict(template_loader=loader,
autoescape="xhtml_escape",
cookie_secret=self.COOKIE_SECRET)
示例6: test_extends
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_extends(self):
loader = DictLoader(
{
"base.html": """\
<title>{% block title %}default title{% end %}</title>
<body>{% block body %}default body{% end %}</body>
""",
"page.html": """\
{% extends "base.html" %}
{% block title %}page title{% end %}
{% block body %}page body{% end %}
""",
}
)
self.assertEqual(
loader.load("page.html").generate(),
b"<title>page title</title>\n<body>page body</body>\n",
)
示例7: test_error_line_number_module
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [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)
示例8: test_default_off
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_default_off(self):
loader = DictLoader(self.templates, autoescape=None)
name = "Bobby <table>s"
self.assertEqual(
loader.load("escaped.html").generate(name=name), b"Bobby <table>s"
)
self.assertEqual(
loader.load("unescaped.html").generate(name=name), b"Bobby <table>s"
)
self.assertEqual(
loader.load("default.html").generate(name=name), b"Bobby <table>s"
)
self.assertEqual(
loader.load("include.html").generate(name=name),
b"escaped: Bobby <table>s\n"
b"unescaped: Bobby <table>s\n"
b"default: Bobby <table>s\n",
)
示例9: test_default_on
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_default_on(self):
loader = DictLoader(self.templates, autoescape="xhtml_escape")
name = "Bobby <table>s"
self.assertEqual(
loader.load("escaped.html").generate(name=name), b"Bobby <table>s"
)
self.assertEqual(
loader.load("unescaped.html").generate(name=name), b"Bobby <table>s"
)
self.assertEqual(
loader.load("default.html").generate(name=name), b"Bobby <table>s"
)
self.assertEqual(
loader.load("include.html").generate(name=name),
b"escaped: Bobby <table>s\n"
b"unescaped: Bobby <table>s\n"
b"default: Bobby <table>s\n",
)
示例10: test_manual_minimize_whitespace
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_manual_minimize_whitespace(self):
# Whitespace including newlines is allowed within template tags
# and directives, and this is one way to avoid long lines while
# keeping extra whitespace out of the rendered output.
loader = DictLoader(
{
"foo.txt": """\
{% for i in items
%}{% if i > 0 %}, {% end %}{#
#}{{i
}}{% end
%}"""
}
)
self.assertEqual(
loader.load("foo.txt").generate(items=range(5)), b"0, 1, 2, 3, 4"
)
示例11: test_whitespace_by_filename
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_whitespace_by_filename(self):
# Default whitespace handling depends on the template filename.
loader = DictLoader(
{
"foo.html": " \n\t\n asdf\t ",
"bar.js": " \n\n\n\t qwer ",
"baz.txt": "\t zxcv\n\n",
"include.html": " {% include baz.txt %} \n ",
"include.txt": "\t\t{% include foo.html %} ",
}
)
# HTML and JS files have whitespace compressed by default.
self.assertEqual(loader.load("foo.html").generate(), b"\nasdf ")
self.assertEqual(loader.load("bar.js").generate(), b"\nqwer ")
# TXT files do not.
self.assertEqual(loader.load("baz.txt").generate(), b"\t zxcv\n\n")
# Each file maintains its own status even when included in
# a file of the other type.
self.assertEqual(loader.load("include.html").generate(), b" \t zxcv\n\n\n")
self.assertEqual(loader.load("include.txt").generate(), b"\t\t\nasdf ")
示例12: get_handlers
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def get_handlers(self):
test = self
class FinishHandler(RequestHandler):
@gen.coroutine
def get(self):
test.final_return = self.finish()
yield test.final_return
@gen.coroutine
def post(self):
self.write("hello,")
yield self.flush()
test.final_return = self.finish("world")
yield test.final_return
class RenderHandler(RequestHandler):
def create_template_loader(self, path):
return DictLoader({"foo.html": "hi"})
@gen.coroutine
def get(self):
test.final_return = self.render("foo.html")
return [("/finish", FinishHandler), ("/render", RenderHandler)]
示例13: get_app_kwargs
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def get_app_kwargs(self):
loader = DictLoader(
{
"linkify.html": "{% module linkify(message) %}",
"page.html": """\
<html><head></head><body>
{% for e in entries %}
{% module Template("entry.html", entry=e) %}
{% end %}
</body></html>""",
"entry.html": """\
{{ set_resources(embedded_css=".entry { margin-bottom: 1em; }",
embedded_javascript="js_embed()",
css_files=["/base.css", "/foo.css"],
javascript_files="/common.js",
html_head="<meta>",
html_body='<script src="/analytics.js"/>') }}
<div class="entry">...</div>""",
}
)
return dict(
template_loader=loader,
autoescape="xhtml_escape",
cookie_secret=self.COOKIE_SECRET,
)
示例14: test_include
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_include(self):
loader = DictLoader({
"index.html": '{% include "header.html" %}\nbody text',
"header.html": "header text",
})
self.assertEqual(loader.load("index.html").generate(),
b"header text\nbody text")
示例15: test_extends
# 需要导入模块: from tornado import template [as 别名]
# 或者: from tornado.template import DictLoader [as 别名]
def test_extends(self):
loader = DictLoader({
"base.html": """\
<title>{% block title %}default title{% end %}</title>
<body>{% block body %}default body{% end %}</body>
""",
"page.html": """\
{% extends "base.html" %}
{% block title %}page title{% end %}
{% block body %}page body{% end %}
""",
})
self.assertEqual(loader.load("page.html").generate(),
b"<title>page title</title>\n<body>page body</body>\n")