本文整理汇总了Python中choco.template.Template.get_def方法的典型用法代码示例。如果您正苦于以下问题:Python Template.get_def方法的具体用法?Python Template.get_def怎么用?Python Template.get_def使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类choco.template.Template
的用法示例。
在下文中一共展示了Template.get_def方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_toplevel
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import get_def [as 别名]
def test_toplevel(self):
"""test calling a def from the top level"""
template = Template("""
this is the body
<%def name="a()">
this is a
</%def>
<%def name="b(x, y)">
this is b, ${x} ${y}
</%def>
""")
self._do_test(template.get_def("a"),
"this is a",
filters=flatten_result)
self._do_test(template.get_def("b"),
"this is b, 10 15",
template_args={'x': 10, 'y': 15},
filters=flatten_result)
self._do_test(template.get_def("body"),
"this is the body",
filters=flatten_result)
# test that args outside of the dict can be used
self._do_test(template.get_def("a"), "this is a",
filters=flatten_result,
template_args={'q': 5, 'zq': 'test'})
示例2: test_def_operations
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import get_def [as 别名]
def test_def_operations(self):
"""test get/list/has def"""
template = Template("""
this is the body
<%def name="a()">
this is a
</%def>
<%def name="b(x, y)">
this is b, ${x} ${y}
</%def>
""")
assert template.get_def("a")
assert template.get_def("b")
assert_raises(AttributeError,
template.get_def,
("c")
)
assert template.has_def("a")
assert template.has_def("b")
assert not template.has_def("c")
defs = template.list_defs()
assert "a" in defs
assert "b" in defs
assert "body" in defs
assert "c" not in defs
示例3: test_toplevel_contextual
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import get_def [as 别名]
def test_toplevel_contextual(self):
template = Template("""
<%!
def bar(fn):
def decorate(context):
context.write("BAR")
fn()
context.write("BAR")
return ''
return decorate
%>
<%def name="foo()" decorator="bar">
this is foo
</%def>
${foo()}
""")
assert flatten_result(template.render()) == "BAR this is foo BAR"
assert flatten_result(template.get_def('foo').render()) == "BAR this is foo BAR"