本文整理汇总了Python中ambari_jinja2.Environment.from_string方法的典型用法代码示例。如果您正苦于以下问题:Python Environment.from_string方法的具体用法?Python Environment.from_string怎么用?Python Environment.from_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ambari_jinja2.Environment
的用法示例。
在下文中一共展示了Environment.from_string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nonvolatile
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_nonvolatile(self):
env = Environment(extensions=['ambari_jinja2.ext.autoescape'],
autoescape=True)
tmpl = env.from_string('{{ {"foo": "<test>"}|xmlattr|escape }}')
assert tmpl.render() == ' foo="<test>"'
tmpl = env.from_string('{% autoescape false %}{{ {"foo": "<test>"}'
'|xmlattr|escape }}{% endautoescape %}')
assert tmpl.render() == ' foo="&lt;test&gt;"'
示例2: test_strict_undefined
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_strict_undefined(self):
env = Environment(undefined=StrictUndefined)
self.assert_raises(UndefinedError, env.from_string('{{ missing }}').render)
self.assert_raises(UndefinedError, env.from_string('{{ missing.attribute }}').render)
self.assert_raises(UndefinedError, env.from_string('{{ missing|list }}').render)
self.assert_equal(env.from_string('{{ missing is not defined }}').render(), 'True')
self.assert_raises(UndefinedError, env.from_string('{{ foo.missing }}').render, foo=42)
self.assert_raises(UndefinedError, env.from_string('{{ not missing }}').render)
示例3: test_finalizer
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_finalizer(self):
def finalize_none_empty(value):
if value is None:
value = u''
return value
env = Environment(finalize=finalize_none_empty)
tmpl = env.from_string('{% for item in seq %}|{{ item }}{% endfor %}')
assert tmpl.render(seq=(None, 1, "foo")) == '||1|foo'
tmpl = env.from_string('<{{ none }}>')
assert tmpl.render() == '<>'
示例4: test_replace
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_replace(self):
env = Environment()
tmpl = env.from_string('{{ string|replace("o", 42) }}')
assert tmpl.render(string='<foo>') == '<f4242>'
env = Environment(autoescape=True)
tmpl = env.from_string('{{ string|replace("o", 42) }}')
assert tmpl.render(string='<foo>') == '<f4242>'
tmpl = env.from_string('{{ string|replace("<", 42) }}')
assert tmpl.render(string='<foo>') == '42foo>'
tmpl = env.from_string('{{ string|replace("o", ">x<") }}')
assert tmpl.render(string=Markup('foo')) == 'f>x<>x<'
示例5: test_volatile_scoping
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_volatile_scoping(self):
env = Environment(extensions=['ambari_jinja2.ext.autoescape'])
tmplsource = '''
{% autoescape val %}
{% macro foo(x) %}
[{{ x }}]
{% endmacro %}
{{ foo().__class__.__name__ }}
{% endautoescape %}
{{ '<testing>' }}
'''
tmpl = env.from_string(tmplsource)
assert tmpl.render(val=True).split()[0] == 'Markup'
assert tmpl.render(val=False).split()[0] == unicode.__name__
# looking at the source we should see <testing> there in raw
# (and then escaped as well)
env = Environment(extensions=['ambari_jinja2.ext.autoescape'])
pysource = env.compile(tmplsource, raw=True)
assert '<testing>\\n' in pysource
env = Environment(extensions=['ambari_jinja2.ext.autoescape'],
autoescape=True)
pysource = env.compile(tmplsource, raw=True)
assert '<testing>\\n' in pysource
示例6: test_loop_controls
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_loop_controls(self):
env = Environment(extensions=['ambari_jinja2.ext.loopcontrols'])
tmpl = env.from_string('''
{%- for item in [1, 2, 3, 4] %}
{%- if item % 2 == 0 %}{% continue %}{% endif -%}
{{ item }}
{%- endfor %}''')
assert tmpl.render() == '13'
tmpl = env.from_string('''
{%- for item in [1, 2, 3, 4] %}
{%- if item > 2 %}{% break %}{% endif -%}
{{ item }}
{%- endfor %}''')
assert tmpl.render() == '12'
示例7: test_erb_syntax
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_erb_syntax(self):
env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
tmpl = env.from_string('''\
<%# I'm a comment, I'm not interesting %>\
<% for item in seq -%>
<%= item %>
<%- endfor %>''')
assert tmpl.render(seq=range(5)) == '01234'
示例8: test_php_syntax
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_php_syntax(self):
env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->')
tmpl = env.from_string('''\
<!-- I'm a comment, I'm not interesting -->\
<? for item in seq -?>
<?= item ?>
<?- endfor ?>''')
assert tmpl.render(seq=range(5)) == '01234'
示例9: test_comment_syntax
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_comment_syntax(self):
env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
tmpl = env.from_string('''\
<!--# I'm a comment, I'm not interesting -->\
<!-- for item in seq --->
${item}
<!--- endfor -->''')
assert tmpl.render(seq=range(5)) == '01234'
示例10: test_do
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_do(self):
env = Environment(extensions=['ambari_jinja2.ext.do'])
tmpl = env.from_string('''
{%- set items = [] %}
{%- for char in "foo" %}
{%- do items.append(loop.index0 ~ char) %}
{%- endfor %}{{ items|join(', ') }}''')
assert tmpl.render() == '0f, 1o, 2o'
示例11: test_super_in_scoped_block
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_super_in_scoped_block(self):
env = Environment(loader=DictLoader({
'master.html': '{% for item in seq %}[{% block item scoped %}'
'{{ item }}{% endblock %}]{% endfor %}'
}))
t = env.from_string('{% extends "master.html" %}{% block item %}'
'{{ super() }}|{{ item * 2 }}{% endblock %}')
assert t.render(seq=range(5)) == '[0|0][1|2][2|4][3|6][4|8]'
示例12: test_join
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_join(self):
tmpl = env.from_string('{{ [1, 2, 3]|join("|") }}')
out = tmpl.render()
assert out == '1|2|3'
env2 = Environment(autoescape=True)
tmpl = env2.from_string('{{ ["<foo>", "<span>foo</span>"|safe]|join }}')
assert tmpl.render() == '<foo><span>foo</span>'
示例13: test_line_syntax
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_line_syntax(self):
env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%')
tmpl = env.from_string('''\
<%# regular comment %>
% for item in seq:
${item}
% endfor''')
assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \
range(5)
env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##')
tmpl = env.from_string('''\
<%# regular comment %>
% for item in seq:
${item} ## the rest of the stuff
% endfor''')
assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \
range(5)
示例14: test_autoescape_support
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_autoescape_support(self):
env = Environment(extensions=['ambari_jinja2.ext.autoescape',
'ambari_jinja2.ext.i18n'])
env.install_gettext_callables(lambda x: u'<strong>Wert: %(name)s</strong>',
lambda s, p, n: s, newstyle=True)
t = env.from_string('{% autoescape ae %}{{ gettext("foo", name='
'"<test>") }}{% endautoescape %}')
assert t.render(ae=True) == '<strong>Wert: <test></strong>'
assert t.render(ae=False) == '<strong>Wert: <test></strong>'
示例15: test_line_syntax_priority
# 需要导入模块: from ambari_jinja2 import Environment [as 别名]
# 或者: from ambari_jinja2.Environment import from_string [as 别名]
def test_line_syntax_priority(self):
# XXX: why is the whitespace there in front of the newline?
env = Environment('{%', '%}', '${', '}', '/*', '*/', '##', '#')
tmpl = env.from_string('''\
/* ignore me.
I'm a multiline comment */
## for item in seq:
* ${item} # this is just extra stuff
## endfor''')
assert tmpl.render(seq=[1, 2]).strip() == '* 1\n* 2'
env = Environment('{%', '%}', '${', '}', '/*', '*/', '#', '##')
tmpl = env.from_string('''\
/* ignore me.
I'm a multiline comment */
# for item in seq:
* ${item} ## this is just extra stuff
## extra stuff i just want to ignore
# endfor''')
assert tmpl.render(seq=[1, 2]).strip() == '* 1\n\n* 2'