本文整理匯總了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