本文整理汇总了Python中mako.lookup.TemplateLookup方法的典型用法代码示例。如果您正苦于以下问题:Python lookup.TemplateLookup方法的具体用法?Python lookup.TemplateLookup怎么用?Python lookup.TemplateLookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mako.lookup
的用法示例。
在下文中一共展示了lookup.TemplateLookup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def __init__(self, extra_vars_func=None, options=None, extension="mak"):
self.extra_vars_func = extra_vars_func
self.extension = extension
if not options:
options = {}
# Pull the options out and initialize the lookup
lookup_options = {}
for k, v in options.items():
if k.startswith("mako."):
lookup_options[k[5:]] = v
elif k in ["directories", "filesystem_checks", "module_directory"]:
lookup_options[k] = v
self.lookup = TemplateLookup(**lookup_options)
self.tmpl_options = {}
# transfer lookup args to template args, based on those available
# in getargspec
for kw in compat.inspect_getargspec(Template.__init__)[0]:
if kw in lookup_options:
self.tmpl_options[kw] = lookup_options[kw]
示例2: __init__
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def __init__(self, extra_vars_func=None, options=None, extension='mak'):
self.extra_vars_func = extra_vars_func
self.extension = extension
if not options:
options = {}
# Pull the options out and initialize the lookup
lookup_options = {}
for k, v in options.items():
if k.startswith('mako.'):
lookup_options[k[5:]] = v
elif k in ['directories', 'filesystem_checks', 'module_directory']:
lookup_options[k] = v
self.lookup = TemplateLookup(**lookup_options)
self.tmpl_options = {}
# transfer lookup args to template args, based on those available
# in getargspec
for kw in compat.inspect_getargspec(Template.__init__)[0]:
if kw in lookup_options:
self.tmpl_options[kw] = lookup_options[kw]
示例3: test_dynamic
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_dynamic(self):
collection = lookup.TemplateLookup()
collection.put_string(
"a",
"""
<%namespace name="b" file="${context['b_def']}"/>
a. b: ${b.body()}
""",
)
collection.put_string(
"b",
"""
b.
""",
)
eq_(
flatten_result(collection.get_template("a").render(b_def="b")),
"a. b: b.",
)
示例4: test_module
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_module(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace name="comp" module="test.sample_module_namespace"/>
this is main. ${comp.foo1()}
${comp.foo2("hi")}
""",
)
assert (
flatten_result(collection.get_template("main.html").render())
== "this is main. this is foo1. this is foo2, x is hi"
)
示例5: test_module_imports
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_module_imports(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace import="*" module="test.foo.test_ns"/>
this is main. ${foo1()}
${foo2("hi")}
""",
)
assert (
flatten_result(collection.get_template("main.html").render())
== "this is main. this is foo1. this is foo2, x is hi"
)
示例6: test_module_imports_2
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_module_imports_2(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace import="foo1, foo2" module="test.foo.test_ns"/>
this is main. ${foo1()}
${foo2("hi")}
""",
)
assert (
flatten_result(collection.get_template("main.html").render())
== "this is main. this is foo1. this is foo2, x is hi"
)
示例7: test_getattr
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_getattr(self):
collection = lookup.TemplateLookup()
collection.put_string(
"main.html",
"""
<%namespace name="foo" file="ns.html"/>
<%
if hasattr(foo, 'lala'):
foo.lala()
if not hasattr(foo, 'hoho'):
context.write('foo has no hoho.')
%>
""",
)
collection.put_string(
"ns.html",
"""
<%def name="lala()">this is lala.</%def>
""",
)
assert (
flatten_result(collection.get_template("main.html").render())
== "this is lala.foo has no hoho."
)
示例8: test_loop_disabled_override_lookup
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_loop_disabled_override_lookup(self):
l = TemplateLookup(enable_loop=False)
l.put_string(
"x",
"""
<%page enable_loop="True" />
% for i in (1, 2, 3):
${i} ${loop.index}
% endfor
""",
)
self._do_test(
l.get_template("x"),
"1 0 2 1 3 2",
template_args=dict(loop="hi"),
filters=flatten_result,
)
示例9: test_loop_enabled_override_lookup
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_loop_enabled_override_lookup(self):
l = TemplateLookup()
l.put_string(
"x",
"""
<%page enable_loop="True" />
% for i in (1, 2, 3):
${i} ${loop.index}
% endfor
""",
)
self._do_test(
l.get_template("x"),
"1 0 2 1 3 2",
template_args=dict(),
filters=flatten_result,
)
示例10: test_block_args
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_block_args(self):
l = TemplateLookup()
l.put_string(
"caller",
"""
<%include file="callee" args="val1='3', val2='4'"/>
""",
)
l.put_string(
"callee",
"""
<%page args="val1, val2"/>
<%block name="foob" args="val1, val2">
foob, ${val1}, ${val2}
</%block>
""",
)
self._do_test(
l.get_template("caller"), ["foob, 3, 4"], filters=result_lines
)
示例11: test_block_pageargs
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_block_pageargs(self):
l = TemplateLookup()
l.put_string(
"caller",
"""
<%include file="callee" args="val1='3', val2='4'"/>
""",
)
l.put_string(
"callee",
"""
<%block name="foob">
foob, ${pageargs['val1']}, ${pageargs['val2']}
</%block>
""",
)
self._do_test(
l.get_template("caller"), ["foob, 3, 4"], filters=result_lines
)
示例12: test_format_exceptions_pygments
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_format_exceptions_pygments(self):
l = TemplateLookup(format_exceptions=True)
l.put_string(
"foo.html",
"""
<%inherit file="base.html"/>
${foobar}
""",
)
l.put_string(
"base.html",
"""
${self.body()}
""",
)
assert (
'<div class="sourceline"><table class="syntax-highlightedtable">'
in l.get_template("foo.html").render_unicode()
)
示例13: test_format_exceptions_no_pygments
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_format_exceptions_no_pygments(self):
l = TemplateLookup(format_exceptions=True)
l.put_string(
"foo.html",
"""
<%inherit file="base.html"/>
${foobar}
""",
)
l.put_string(
"base.html",
"""
${self.body()}
""",
)
assert '<div class="sourceline">${foobar}</div>' in result_lines(
l.get_template("foo.html").render_unicode()
)
示例14: test_utf8_format_exceptions_pygments
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_utf8_format_exceptions_pygments(self):
"""test that htmlentityreplace formatting is applied to
exceptions reported with format_exceptions=True"""
l = TemplateLookup(format_exceptions=True)
if compat.py3k:
l.put_string(
"foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}"""
)
else:
l.put_string(
"foo.html",
"""# -*- coding: utf-8 -*-\n${u'привет' + foobar}""",
)
if compat.py3k:
assert "'привет'</span>" in l.get_template(
"foo.html"
).render().decode("utf-8")
else:
assert (
"'прив"
"ет'</span>"
) in l.get_template("foo.html").render().decode("utf-8")
示例15: test_module_block_line_number
# 需要导入模块: from mako import lookup [as 别名]
# 或者: from mako.lookup import TemplateLookup [as 别名]
def test_module_block_line_number(self):
l = TemplateLookup()
l.put_string(
"foo.html",
"""
<%!
def foo():
msg = "Something went wrong."
raise RuntimeError(msg) # This is the line.
%>
${foo()}
""",
)
t = l.get_template("foo.html")
try:
t.render()
except:
text_error = exceptions.text_error_template().render_unicode()
assert 'File "foo.html", line 7, in render_body' in text_error
assert 'File "foo.html", line 5, in foo' in text_error
assert "raise RuntimeError(msg) # This is the line." in text_error
else:
assert False