本文整理汇总了Python中kajiki.MockLoader类的典型用法代码示例。如果您正苦于以下问题:Python MockLoader类的具体用法?Python MockLoader怎么用?Python MockLoader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MockLoader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_import_auto
def test_import_auto(self):
lib = TextTemplate(
"""%def evenness(n)
%if n % 2 == 0
even\\
%else
odd\\
%end
%end
%def half_evenness(n)
half of $n is ${evenness(n/2)}\\
%end"""
)
tpl = TextTemplate(
"""%import "lib.txt"
%for i in range(4)
$i is ${lib.evenness(i)}${lib.half_evenness(i)}
%end"""
)
loader = MockLoader({"lib.txt": lib, "tpl.txt": tpl})
tpl = loader.import_("tpl.txt")
rsp = tpl(dict(name="Rick")).render()
assert (
rsp == "0 is even half of 0 is even\n"
"1 is odd half of 1 is odd\n"
"2 is even half of 2 is odd\n"
"3 is odd half of 3 is odd\n"
), rsp
示例2: test_substituting_gettext_with_lambda_extending
def test_substituting_gettext_with_lambda_extending(self):
gettext = lambda x: 'egg'
loader = MockLoader({
'parent.html': XMLTemplate('''<div>parent</div>'''),
'child.html': XMLTemplate('''<py:extends href="parent.html"><div>child</div></py:extends>''')})
tpl = loader.import_('child.html')
rsp = tpl(dict(gettext=gettext)).render()
assert rsp == '''<div>egg</div><div>egg</div>''', rsp
示例3: test_substituting_gettext_with_lambda_extending_twice
def test_substituting_gettext_with_lambda_extending_twice(self):
gettext = lambda x: 'egg'
loader = MockLoader({
'parent.html': XMLTemplate('''<div>parent</div>'''),
'mid.html': XMLTemplate('''<py:extends href="parent.html"><div>${variable}</div></py:extends>'''),
'child.html': XMLTemplate('''<py:extends href="mid.html"><div>child</div></py:extends>''')})
tpl = loader.import_('child.html')
rsp = tpl(dict(variable='spam', gettext=gettext)).render()
# variables must not be translated
assert rsp == '''<div>egg</div><div>spam</div><div>egg</div>''', rsp
示例4: test_include
def test_include(self):
loader = MockLoader({
'hdr.txt': TextTemplate('# header\n'),
'tpl.txt': TextTemplate('''a
%include "hdr.txt"
b
''')})
tpl = loader.import_('tpl.txt')
rsp = tpl(dict(name='Rick')).render()
assert rsp == 'a\n# header\nb\n', rsp
示例5: test_block
def test_block(self):
loader = MockLoader(
{
"parent.html": XMLTemplate(
"""<div
><py:def function="greet(name)"
>Hello, $name!</py:def
><py:def function="sign(name)"
>Sincerely,<br/><em>$name</em></py:def
>${greet(to)}
<p py:block="body">It was good seeing you last Friday.
Thanks for the gift!</p>
${sign(from_)}
</div>"""
),
"child.html": XMLTemplate(
"""<py:extends href="parent.html"
><py:def function="greet(name)"
>Dear $name:</py:def
><py:block name="body">${parent_block()}
<p>And don't forget you owe me money!</p>
</py:block
></py:extends>
"""
),
}
)
parent = loader.import_("parent.html")
rsp = parent({"to": "Mark", "from_": "Rick"}).render()
assert (
rsp
== """<div>Hello, Mark!
<p>It was good seeing you last Friday.
Thanks for the gift!</p>
Sincerely,<br/><em>Rick</em>
</div>"""
), rsp
child = loader.import_("child.html")
rsp = child({"to": "Mark", "from_": "Rick"}).render()
assert (
rsp
== """<div>Dear Mark:
<p>It was good seeing you last Friday.
Thanks for the gift!</p>
<p>And don't forget you owe me money!</p>
Sincerely,<br/><em>Rick</em>
</div>"""
), rsp
示例6: test_autoblocks_disabling
def test_autoblocks_disabling(self):
loader = MockLoader(
{
"parent.html": XMLTemplate(
"""
<html py:strip="">
<head></head>
<body py:autoblock="False">
<p py:block="body">It was good seeing you last Friday.
Thanks for the gift!</p>
</body>
</html>""",
autoblocks=["body"],
),
"child.html": XMLTemplate(
"""
<html>
<py:extends href="parent.html"/>
<body><em>Great conference this weekend!</em></body>
</html>""",
autoblocks=["body"],
),
}
)
parent = loader.import_("parent.html")
rsp = parent().render()
assert (
rsp
== """
<head/>
<body>
<p>It was good seeing you last Friday.
Thanks for the gift!</p>
</body>
"""
), rsp
child = loader.import_("child.html")
rsp = child().render()
assert (
rsp
== """<html>
<head/>
<body>
<em>Great conference this weekend!</em>
</body>
</html>"""
), rsp
示例7: test_basic
def test_basic(self):
parent = TextTemplate(
"""
%def header()
# Header name=$name
%end
%def footer()
# Footer
%end
%def body()
## Parent Body
id() = ${id()}
local.id() = ${local.id()}
self.id() = ${self.id()}
child.id() = ${child.id()}
%end
%def id()
parent\\
%end
${header()}${body()}${footer()}"""
)
mid = TextTemplate(
"""%extends "parent.txt"
%def id()
mid\\
%end
"""
)
child = TextTemplate(
"""%extends "mid.txt"
%def id()
child\\
%end
%def body()
## Child Body
${parent.body()}\\
%end
"""
)
loader = MockLoader({"parent.txt": parent, "mid.txt": mid, "child.txt": child})
tpl = loader.import_("child.txt")
rsp = tpl(dict(name="Rick")).render()
assert (
rsp == "# Header name=Rick\n"
"## Child Body\n"
"## Parent Body\n"
"id() = child\n"
"local.id() = parent\n"
"self.id() = child\n"
"child.id() = mid\n"
"# Footer\n"
), rsp
示例8: test_basic
def test_basic(self):
loader = MockLoader(
{
"parent.html": XMLTemplate(
"""<div
><h1 py:def="header()">Header name=$name</h1
><h6 py:def="footer()">Footer</h6
><div py:def="body()">
id() = ${id()}
local.id() = ${local.id()}
self.id() = ${self.id()}
child.id() = ${child.id()}
</div><span py:def="id()">parent</span>
${header()}
${body()}
${footer()}
</div>"""
),
"mid.html": XMLTemplate(
"""<py:extends href="parent.html"
><span py:def="id()">mid</span
></py:extends>"""
),
"child.html": XMLTemplate(
"""<py:extends href="mid.html"
><span py:def="id()">child</span
><div py:def="body()">
<h2>Child Body</h2>
${parent.body()}
</div></py:extends>"""
),
}
)
tpl = loader.import_("child.html")
rsp = tpl(dict(name="Rick")).render()
assert (
rsp
== """<div>
<h1>Header name=Rick</h1>
<div>
<h2>Child Body</h2>
<div>
id() = <span>child</span>
local.id() = <span>parent</span>
self.id() = <span>child</span>
child.id() = <span>mid</span>
</div>
</div>
<h6>Footer</h6>
</div>"""
), rsp
示例9: test_include
def test_include(self):
loader = MockLoader({
'hdr.html':XMLTemplate('<h1>Header</h1>\n'),
'tpl.html':XMLTemplate('''<html><body>
<py:include href="hdr.html"/>
<p>This is the body</p>
</body></html>''')
})
tpl = loader.import_('tpl.html')
rsp = tpl(dict(name='Rick')).render()
assert rsp == '''<html><body>
<h1>Header</h1>
<p>This is the body</p>
</body></html>''', rsp
示例10: test_include
def test_include(self):
'''Must NOT result in: NameError: global name 'name' is not defined'''
loader = MockLoader({
'included.html': XMLTemplate('<p>The included template must also '
'access Kajiki globals and the template context: '
'${value_of("name")}</p>\n'),
'tpl.html': XMLTemplate('<html><body><p>This is the body</p>\n'
'<py:include href="included.html"/></body></html>')
})
tpl = loader.import_('tpl.html')
rsp = tpl(dict(name='Rick')).render()
assert ('<html><body><p>This is the body</p>\n'
'<p>The included template must also access Kajiki globals and '
'the template context: Rick</p></body></html>' == rsp)
示例11: test_dynamic
def test_dynamic(self):
loader = MockLoader({
'parent0.html': XMLTemplate('<span>Parent 0</span>'),
'parent1.html': XMLTemplate('<span>Parent 1</span>'),
'child.html': XMLTemplate('''<div
><py:if test="p == 0"><py:extends href="parent0.html"/></py:if
><py:else><py:extends href="parent1.html"/></py:else
></div>
''')
})
tpl = loader.import_('child.html')
rsp = tpl(dict(p=0)).render()
assert rsp == '<div><span>Parent 0</span></div>', rsp
rsp = tpl(dict(p=1)).render()
assert rsp == '<div><span>Parent 1</span></div>', rsp
示例12: test_include
def test_include(self):
loader = MockLoader(
{
"hdr.txt": TextTemplate("# header\n"),
"tpl.txt": TextTemplate(
"""a
%include "hdr.txt"
b
"""
),
}
)
tpl = loader.import_("tpl.txt")
rsp = tpl(dict(name="Rick")).render()
assert rsp == "a\n# header\nb\n", rsp
示例13: test_dynamic
def test_dynamic(self):
loader = MockLoader({
'parent0.txt': TextTemplate('Parent 0'),
'parent1.txt': TextTemplate('Parent 1'),
'child.txt': TextTemplate('''%if p == 0
%extends "parent0.txt"
%else
%extends "parent1.txt"
%end
''')
})
tpl = loader.import_('child.txt')
rsp = tpl(dict(p=0)).render()
assert rsp == 'Parent 0', rsp
rsp = tpl(dict(p=1)).render()
assert rsp == 'Parent 1', rsp
示例14: test_basic
def test_basic(self):
parent = TextTemplate('''
%def header()
# Header name=$name
%end
%def footer()
# Footer
%end
%def body()
## Parent Body
id() = ${id()}
local.id() = ${local.id()}
self.id() = ${self.id()}
child.id() = ${child.id()}
%end
%def id()
parent\\
%end
${header()}${body()}${footer()}''')
mid = TextTemplate('''%extends "parent.txt"
%def id()
mid\\
%end
''')
child = TextTemplate('''%extends "mid.txt"
%def id()
child\\
%end
%def body()
## Child Body
${parent.body()}\\
%end
''')
loader = MockLoader({
'parent.txt': parent,
'mid.txt': mid,
'child.txt': child})
tpl = loader.import_('child.txt')
rsp = tpl(dict(name='Rick')).render()
assert (rsp == '# Header name=Rick\n'
'## Child Body\n'
'## Parent Body\n'
'id() = child\n'
'local.id() = parent\n'
'self.id() = child\n'
'child.id() = mid\n'
'# Footer\n'), rsp
示例15: test_block
def test_block(self):
loader = MockLoader(
{
"parent.txt": TextTemplate(
"""%def greet(name)
Hello, $name!\\
%end
%def sign(name)
Sincerely,
$name\\
%end
${greet(to)}
%block body
It was good seeing you last Friday. Thanks for the gift!
%end
${sign(from_)}
"""
),
"child.txt": TextTemplate(
"""%extends "parent.txt"
%def greet(name)
Dear $name:\\
%end
%block body
${parent_block()}\\
And don't forget you owe me money!
%end
"""
),
}
)
child = loader.import_("child.txt")
rsp = child({"to": "Mark", "from_": "Rick"}).render()
assert (
rsp
== """Dear Mark:
It was good seeing you last Friday. Thanks for the gift!
And don't forget you owe me money!
Sincerely,
Rick
"""
), rsp