当前位置: 首页>>代码示例>>Python>>正文


Python Environment.from_string方法代码示例

本文整理汇总了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="&lt;test&gt;"'
     tmpl = env.from_string('{% autoescape false %}{{ {"foo": "<test>"}'
                            '|xmlattr|escape }}{% endautoescape %}')
     assert tmpl.render() == ' foo=&#34;&amp;lt;test&amp;gt;&#34;'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:ext.py

示例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)
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:api.py

示例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() == '<>'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:12,代码来源:api.py

示例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>') == '&lt;f4242&gt;'
     tmpl = env.from_string('{{ string|replace("<", 42) }}')
     assert tmpl.render(string='<foo>') == '42foo&gt;'
     tmpl = env.from_string('{{ string|replace("o", ">x<") }}')
     assert tmpl.render(string=Markup('foo')) == 'f&gt;x&lt;&gt;x&lt;'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:13,代码来源:filters.py

示例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 '&lt;testing&gt;\\n' in pysource
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:27,代码来源:ext.py

示例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'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:18,代码来源:ext.py

示例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'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:lexnparse.py

示例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'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:lexnparse.py

示例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'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:lexnparse.py

示例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'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:ext.py

示例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]'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:inheritance.py

示例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() == '&lt;foo&gt;<span>foo</span>'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:10,代码来源:filters.py

示例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)
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:20,代码来源:lexnparse.py

示例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: &lt;test&gt;</strong>'
     assert t.render(ae=False) == '<strong>Wert: <test></strong>'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:11,代码来源:ext.py

示例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'
开发者ID:OpenPOWER-BigData,项目名称:HDP-ambari,代码行数:21,代码来源:lexnparse.py


注:本文中的ambari_jinja2.Environment.from_string方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。