當前位置: 首頁>>代碼示例>>Python>>正文


Python Environment.from_string方法代碼示例

本文整理匯總了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'
    ]
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:11,代碼來源:test_parser.py

示例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()
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:21,代碼來源:test_syntax.py

示例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'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:6,代碼來源:test_parser.py

示例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'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:6,代碼來源:test_parser.py

示例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'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:6,代碼來源:test_parser.py

示例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'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:6,代碼來源:test_parser.py

示例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
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:8,代碼來源:test_security.py

示例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}'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:7,代碼來源:test_various.py

示例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'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:8,代碼來源:test_syntax.py

示例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?'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:32,代碼來源:strange.py

示例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>")
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:8,代碼來源:test_lexer.py

示例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}"
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:7,代碼來源:test_lexer.py

示例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'
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:8,代碼來源:test_i18n.py

示例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() == ''
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:6,代碼來源:test_inheritance.py

示例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 }} &mdash; 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>\
''')
開發者ID:dcramer,項目名稱:jinja1-djangosupport,代碼行數:48,代碼來源:generate.py


注:本文中的jinja.Environment.from_string方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。