本文整理汇总了Python中jinja.Environment.from_string方法的典型用法代码示例。如果您正苦于以下问题:Python Environment.from_string方法的具体用法?Python Environment.from_string怎么用?Python Environment.from_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja.Environment
的用法示例。
在下文中一共展示了Environment.from_string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_no_variable_block
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_no_variable_block():
env = Environment('{%', '%}', None, None)
tmpl = env.from_string(NO_VARIABLE_BLOCK)
assert tmpl.render(foo=42, seq=range(2)).splitlines() == [
'42',
'01',
'foo is 42',
'42 foos'
]
示例2: test_extends_position
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_extends_position():
env = Environment(loader=DictLoader({
'empty': '[{% block empty %}{% endblock %}]'
}))
tests = [
('{% extends "empty" %}', '[!]'),
(' {% extends "empty" %}', '[!]'),
(' !\n', ' !\n!'),
('{# foo #} {% extends "empty" %}', '[!]'),
('{% set foo = "blub" %}{% extends "empty" %}', None)
]
for tmpl, expected_output in tests:
try:
tmpl = env.from_string(tmpl + '{% block empty %}!{% endblock %}')
except TemplateSyntaxError:
assert expected_output is None, 'got syntax error'
else:
assert expected_output == tmpl.render()
示例3: test_smarty_syntax
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_smarty_syntax():
env = Environment('{', '}', '{', '}', '{*', '*}')
tmpl = env.from_string(SMARTY_SYNTAX)
assert tmpl.render(seq=range(5)) == '01234'
示例4: test_comment_syntax
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_comment_syntax():
env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
tmpl = env.from_string(COMMENT_SYNTAX)
assert tmpl.render(seq=range(5)) == '01234'
示例5: test_erb_syntax
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_erb_syntax():
env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
tmpl = env.from_string(ERB_SYNTAX)
assert tmpl.render(seq=range(5)) == '01234'
示例6: test_php_syntax
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_php_syntax():
env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')
tmpl = env.from_string(PHP_SYNTAX)
assert tmpl.render(seq=range(5)) == '01234'
示例7: test_nonlocal_set
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_nonlocal_set():
env = Environment()
env.globals['outer'] = 42
tmpl = env.from_string(NONLOCALSET)
assert tmpl.render() == '9'
assert env.globals['outer'] == 42
示例8: test_crazy_raw
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_crazy_raw():
from jinja import Environment
env = Environment('{', '}', '{', '}')
tmpl = env.from_string('{raw}{broken foo}{endraw}')
assert tmpl.render() == '{broken foo}'
示例9: test_call
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_call():
from jinja import Environment
env = Environment()
env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g
tmpl = env.from_string(CALL)
assert tmpl.render() == 'abdfh'
示例10: TestException
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
{% endblock %}
{% endif %}
"""
class TestException(Exception):
pass
def throw_exception():
raise TestException()
env = Environment(
loader=DictLoader(dict(base=base_tmpl))
)
if __name__ == '__main__':
for name in 'test1', 'test2':
template_body = globals().get(name)
template = env.from_string(template_body)
try:
print 'Rendering template:\n"""%s"""' % template_body
template.render(throw_exception=throw_exception)
except TestException:
print 'Result: throw_exception() was called'
else:
print 'Result: throw_exception() was not called'
print
print 'First template illustrates that condition is working well'
print 'The question is - why {% block %} is being evalueted '\
'in false condition in second template?'
示例11: test_comments
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_comments():
from jinja import Environment
env = Environment('<!--', '-->', '{', '}')
tmpl = env.from_string(COMMENTS)
assert tmpl.render(seq=range(3)) == ("<ul>\n <li>0</li>\n "
"<li>1</li>\n <li>2</li>\n</ul>")
示例12: test_balancing
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_balancing():
from jinja import Environment
env = Environment('{%', '%}', '${', '}')
tmpl = env.from_string(BALANCING)
assert tmpl.render(seq=range(3)) == "{'FOO': 0}{'FOO': 1}{'FOO': 2}"
示例13: test_factory
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_factory():
def factory(context):
return SimpleTranslator(context['LANGUAGE'] or 'en')
env = Environment(translator_factory=factory)
tmpl = env.from_string('{% trans "watch out" %}')
assert tmpl.render(LANGUAGE='de') == 'pass auf'
示例14: test_empty_super
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
def test_empty_super():
env = Environment(loader=DictLoader({'a': '{% block foo %}{% endblock %}'}))
t = env.from_string('{% extends "a" %}{% block foo %}{{ super() }}{% endblock %}')
assert t.render() == ''
示例15:
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import from_string [as 别名]
FULL_TEMPLATE = e.from_string('''\
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>{{ title }} — Jinja Documentation</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">
{{ style|e }}
</style>
</head>
<body>
<div id="content">
{% if file_id == 'index' %}
<div id="jinjalogo"></div>
<h2 class="subheading plain">{{ title }}</h2>
{% else %}
<h1 class="heading"><span>Jinja</span></h1>
<h2 class="subheading">{{ title }}</h2>
{% endif %}
{% if file_id != 'index' or toc %}
<div id="toc">
<h2>Navigation</h2>
<ul>
<li><a href="index.html">back to index</a></li>
</ul>
{% if toc %}
<h2>Contents</h2>
<ul class="contents">
{% for key, value in toc %}
<li><a href="{{ key }}">{{ value }}</a></li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endif %}
<div id="contentwrapper">
{{ body }}
</div>
</div>
</body>
<!-- generated on: {{ generation_date }}
file id: {{ file_id }} -->
</html>\
''')