本文整理汇总了Python中choco.template.Template.render方法的典型用法代码示例。如果您正苦于以下问题:Python Template.render方法的具体用法?Python Template.render怎么用?Python Template.render使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类choco.template.Template
的用法示例。
在下文中一共展示了Template.render方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_strict
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_strict(self):
t = Template("""
% if x is UNDEFINED:
undefined
% else:
x: ${x}
% endif
""", strict_undefined=True)
assert result_lines(t.render(x=12)) == ['x: 12']
assert_raises(
NameError,
t.render, y=12
)
l = TemplateLookup(strict_undefined=True)
l.put_string("a", "some template")
l.put_string("b", """
<%namespace name='a' file='a' import='*'/>
% if x is UNDEFINED:
undefined
% else:
x: ${x}
% endif
""")
assert result_lines(t.render(x=12)) == ['x: 12']
assert_raises(
NameError,
t.render, y=12
)
示例2: test_module_roundtrip
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_module_roundtrip(self):
lookup = TemplateLookup()
template = Template("""
<%inherit file="base.html"/>
% for x in range(5):
${x}
% endfor
""", lookup=lookup)
base = Template("""
This is base.
${self.body()}
""", lookup=lookup)
lookup.put_template("base.html", base)
lookup.put_template("template.html", template)
assert result_lines(template.render()) == [
"This is base.", "0", "1", "2", "3", "4"
]
lookup = TemplateLookup()
template = ModuleTemplate(template.module, lookup=lookup)
base = ModuleTemplate(base.module, lookup=lookup)
lookup.put_template("base.html", base)
lookup.put_template("template.html", template)
assert result_lines(template.render()) == [
"This is base.", "0", "1", "2", "3", "4"
]
示例3: test_invalidate
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_invalidate(self):
t = Template("""
<%%def name="foo()" cached="True">
foo: ${x}
</%%def>
<%%def name="bar()" cached="True" cache_type='dbm' cache_dir='%s'>
bar: ${x}
</%%def>
${foo()} ${bar()}
""" % module_base)
self._install_mock_cache(t)
assert result_lines(t.render(x=1)) == ["foo: 1", "bar: 1"]
assert result_lines(t.render(x=2)) == ["foo: 1", "bar: 1"]
t.cache.invalidate_def('foo')
assert result_lines(t.render(x=3)) == ["foo: 3", "bar: 1"]
t.cache.invalidate_def('bar')
assert result_lines(t.render(x=4)) == ["foo: 3", "bar: 4"]
t = Template("""
<%%page cached="True" cache_type="dbm" cache_dir="%s"/>
page: ${x}
""" % module_base)
self._install_mock_cache(t)
assert result_lines(t.render(x=1)) == ["page: 1"]
assert result_lines(t.render(x=2)) == ["page: 1"]
t.cache.invalidate_body()
assert result_lines(t.render(x=3)) == ["page: 3"]
assert result_lines(t.render(x=4)) == ["page: 3"]
示例4: test_interpret_expression_from_arg_two
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_interpret_expression_from_arg_two(self):
"""test that cache_key=${foo} gets its value from
the 'foo' argument regardless of it being passed
from the context.
This is here testing that there's no change
to existing behavior before and after #191.
"""
t = Template("""
<%def name="layout(foo)" cached="True" cache_key="${foo}">
foo: ${value}
</%def>
${layout(3)}
""", cache_impl="plain")
eq_(
result_lines(t.render(foo='foo', value=1)),
["foo: 1"]
)
eq_(
result_lines(t.render(foo='bar', value=2)),
["foo: 1"]
)
示例5: test_nested_call_4
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_nested_call_4(self):
base = """
<%def name="A()">
A_def
${caller.body()}
</%def>
<%def name="B()">
B_def
${caller.body()}
</%def>
"""
template = Template(base + """
<%def name="C()">
C_def
<%self:B>
<%self:A>
A_body
</%self:A>
B_body
${caller.body()}
</%self:B>
</%def>
<%self:C>
C_body
</%self:C>
""")
eq_(
flatten_result(template.render()),
"C_def B_def A_def A_body B_body C_body"
)
template = Template(base + """
<%def name="C()">
C_def
<%self:B>
B_body
${caller.body()}
<%self:A>
A_body
</%self:A>
</%self:B>
</%def>
<%self:C>
C_body
</%self:C>
""")
eq_(
flatten_result(template.render()),
"C_def B_def B_body C_body A_def A_body"
)
示例6: test_basic
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_basic(self):
template = Template("""
<%page args="x, y, z=7"/>
this is page, ${x}, ${y}, ${z}
""")
assert flatten_result(template.render(x=5, y=10)) == "this is page, 5, 10, 7"
assert flatten_result(template.render(x=5, y=10, z=32)) == "this is page, 5, 10, 32"
assert_raises(TypeError, template.render, y=10)
示例7: test_builtins
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_builtins(self):
t = Template("""
${"this is <text>" | h}
""")
assert flatten_result(t.render()) == "this is <text>"
t = Template("""
http://foo.com/arg1=${"hi! this is a string." | u}
""")
assert flatten_result(t.render()) == "http://foo.com/arg1=hi%21+this+is+a+string."
示例8: test_dynamic_key_with_funcargs
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_dynamic_key_with_funcargs(self):
t = Template("""
<%def name="foo(num=5)" cached="True" cache_key="foo_${str(num)}">
hi
</%def>
${foo()}
""")
m = self._install_mock_cache(t)
t.render()
t.render()
assert result_lines(t.render()) == ['hi']
assert m.key == "foo_5"
t = Template("""
<%def name="foo(*args, **kwargs)" cached="True"
cache_key="foo_${kwargs['bar']}">
hi
</%def>
${foo(1, 2, bar='lala')}
""")
m = self._install_mock_cache(t)
t.render()
assert result_lines(t.render()) == ['hi']
assert m.key == "foo_lala"
t = Template('''
<%page args="bar='hi'" cache_key="foo_${bar}" cached="True"/>
hi
''')
m = self._install_mock_cache(t)
t.render()
assert result_lines(t.render()) == ['hi']
assert m.key == "foo_hi"
示例9: test_expression_declared
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_expression_declared(self):
t = Template("""
${",".join([t for t in ("a", "b", "c")])}
""", strict_undefined=True)
eq_(result_lines(t.render()), ['a,b,c'])
t = Template("""
<%self:foo value="${[(val, n) for val, n in [(1, 2)]]}"/>
<%def name="foo(value)">
${value}
</%def>
""", strict_undefined=True)
eq_(result_lines(t.render()), ['[(1, 2)]'])
t = Template("""
<%call expr="foo(value=[(val, n) for val, n in [(1, 2)]])" />
<%def name="foo(value)">
${value}
</%def>
""", strict_undefined=True)
eq_(result_lines(t.render()), ['[(1, 2)]'])
l = TemplateLookup(strict_undefined=True)
l.put_string("i", "hi, ${pageargs['y']}")
l.put_string("t", """
<%include file="i" args="y=[x for x in range(3)]" />
""")
eq_(
result_lines(l.get_template("t").render()), ['hi, [0, 1, 2]']
)
l.put_string('q', """
<%namespace name="i" file="${(str([x for x in range(3)][2]) + 'i')[-1]}" />
${i.body(y='x')}
""")
eq_(
result_lines(l.get_template("q").render()), ['hi, x']
)
t = Template("""
<%
y = lambda q: str(q)
%>
${y('hi')}
""", strict_undefined=True)
eq_(
result_lines(t.render()), ["hi"]
)
示例10: test_custom_args_page
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_custom_args_page(self):
t = Template("""
<%page cached="True" cache_region="myregion"
cache_timeout="50" cache_foo="foob"/>
""")
m = self._install_mock_cache(t, "simple")
t.render()
eq_(
m.kwargs,
{'region': 'myregion',
'timeout': 50, 'foo': 'foob'})
示例11: test_no_lookup
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_no_lookup(self):
t = Template("hi <%include file='foo.html'/>")
try:
t.render()
assert False
except errors.TemplateLookupException:
eq_(
str(compat.exception_as()),
"Template 'memory:%s' has no TemplateLookup associated" % \
hex(id(t))
)
示例12: test_pass_context
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_pass_context(self):
t = Template("""
<%page cached="True"/>
""")
m = self._install_mock_cache(t)
t.render()
assert 'context' not in m.kwargs
m.pass_context = True
t.render(x="bar")
assert 'context' in m.kwargs
assert m.kwargs['context'].get('x') == 'bar'
示例13: test_custom_args_def
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_custom_args_def(self):
t = Template("""
<%def name="foo()" cached="True" cache_region="myregion"
cache_timeout="50" cache_foo="foob">
</%def>
${foo()}
""")
m = self._install_mock_cache(t, 'simple')
t.render()
eq_(
m.kwargs,
{'region': 'myregion',
'timeout': 50, 'foo': 'foob'})
示例14: test_cache_uses_current_context
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_cache_uses_current_context(self):
t = Template("""
${foo()}
<%def name="foo()" cached="True" cache_timeout="1">
foo: ${x}
</%def>
""")
self._install_mock_cache(t)
x1 = t.render(x=1)
time.sleep(1.2)
x2 = t.render(x=2)
eq_(x1.strip(), "foo: 1")
eq_(x2.strip(), "foo: 2")
示例15: test_scope_two
# 需要导入模块: from choco.template import Template [as 别名]
# 或者: from choco.template.Template import render [as 别名]
def test_scope_two(self):
t = Template("""
y is ${y}
<%
y = 7
%>
y is ${y}
""")
try:
t.render(y=None)
assert False
except UnboundLocalError:
assert True