python类DictLoader()的实例源码

inheritance.py 文件源码 项目:oa_qian 作者: sunqb 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_fixed_macro_scoping_bug(self):
        assert Environment(loader=DictLoader({
            'test.html': '''\
        {% extends 'details.html' %}

        {% macro my_macro() %}
        my_macro
        {% endmacro %}

        {% block inner_box %}
            {{ my_macro() }}
        {% endblock %}
            ''',
            'details.html': '''\
        {% extends 'standard.html' %}

        {% macro my_macro() %}
        my_macro
        {% endmacro %}

        {% block content %}
            {% block outer_box %}
                outer_box
                {% block inner_box %}
                    inner_box
                {% endblock %}
            {% endblock %}
        {% endblock %}
        ''',
            'standard.html': '''
        {% block content %} {% endblock %}
        '''
        })).get_template("test.html").render().split() == [u'outer_box', u'my_macro']
templating.py 文件源码 项目:chihu 作者: yelongyu 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def test_custom_template_loader(self):
        class MyFlask(flask.Flask):
            def create_global_jinja_loader(self):
                from jinja2 import DictLoader
                return DictLoader({'index.html': 'Hello Custom World!'})
        app = MyFlask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('index.html')
        c = app.test_client()
        rv = c.get('/')
        self.assert_equal(rv.data, b'Hello Custom World!')
templating.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_custom_template_loader(self):
        class MyFlask(flask.Flask):
            def create_global_jinja_loader(self):
                from jinja2 import DictLoader
                return DictLoader({'index.html': 'Hello Custom World!'})
        app = MyFlask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('index.html')
        c = app.test_client()
        rv = c.get('/')
        self.assert_equal(rv.data, b'Hello Custom World!')
imports.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_context_include_with_overrides(self):
        env = Environment(loader=DictLoader(dict(
            main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",
            item="{{ item }}"
        )))
        assert env.get_template("main").render() == "123"
inheritance.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_super(self):
        env = Environment(loader=DictLoader({
            'a': '{% block intro %}INTRO{% endblock %}|'
                 'BEFORE|{% block data %}INNER{% endblock %}|AFTER',
            'b': '{% extends "a" %}{% block data %}({{ '
                 'super() }}){% endblock %}',
            'c': '{% extends "b" %}{% block intro %}--{{ '
                 'super() }}--{% endblock %}\n{% block data '
                 '%}[{{ super() }}]{% endblock %}'
        }))
        tmpl = env.get_template('c')
        assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'
inheritance.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_preserve_blocks(self):
        env = Environment(loader=DictLoader({
            'a': '{% if false %}{% block x %}A{% endblock %}{% endif %}{{ self.x() }}',
            'b': '{% extends "a" %}{% block x %}B{{ super() }}{% endblock %}'
        }))
        tmpl = env.get_template('b')
        assert tmpl.render() == 'BA'
inheritance.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_dynamic_inheritance(self):
        env = Environment(loader=DictLoader({
            'master1': 'MASTER1{% block x %}{% endblock %}',
            'master2': 'MASTER2{% block x %}{% endblock %}',
            'child': '{% extends master %}{% block x %}CHILD{% endblock %}'
        }))
        tmpl = env.get_template('child')
        for m in range(1, 3):
            assert tmpl.render(master='master%d' % m) == 'MASTER%dCHILD' % m
inheritance.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_scoped_block(self):
        env = Environment(loader=DictLoader({
            'master.html': '{% for item in seq %}[{% block item scoped %}'
                           '{% endblock %}]{% endfor %}'
        }))
        t = env.from_string('{% extends "master.html" %}{% block item %}'
                            '{{ item }}{% endblock %}')
        assert t.render(seq=list(range(5))) == '[0][1][2][3][4]'
inheritance.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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=list(range(5))) == '[0|0][1|2][2|4][3|6][4|8]'
inheritance.py 文件源码 项目:pyetje 作者: rorlika 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_fixed_macro_scoping_bug(self):
        assert Environment(loader=DictLoader({
            'test.html': '''\
        {% extends 'details.html' %}

        {% macro my_macro() %}
        my_macro
        {% endmacro %}

        {% block inner_box %}
            {{ my_macro() }}
        {% endblock %}
            ''',
            'details.html': '''\
        {% extends 'standard.html' %}

        {% macro my_macro() %}
        my_macro
        {% endmacro %}

        {% block content %}
            {% block outer_box %}
                outer_box
                {% block inner_box %}
                    inner_box
                {% endblock %}
            {% endblock %}
        {% endblock %}
        ''',
            'standard.html': '''
        {% block content %} {% endblock %}
        '''
        })).get_template("test.html").render().split() == [u'outer_box', u'my_macro']
templating.py 文件源码 项目:tellmeabout.coffee 作者: billyfung 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_custom_template_loader(self):
        class MyFlask(flask.Flask):
            def create_global_jinja_loader(self):
                from jinja2 import DictLoader
                return DictLoader({'index.html': 'Hello Custom World!'})
        app = MyFlask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('index.html')
        c = app.test_client()
        rv = c.get('/')
        self.assert_equal(rv.data, b'Hello Custom World!')
test_jinja2_views.py 文件源码 项目:statik 作者: thanethomson 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __init__(self, templates_dict=TEST_TEMPLATES, base_path="/"):
        self.env = Environment(
            loader=DictLoader(templates_dict),
            extensions=[
                'statik.jinja2ext.StatikUrlExtension',
                'statik.jinja2ext.StatikAssetExtension'
            ]
        )
        self.env.filters['date'] = filter_datetime
        self.env.statik_base_url = base_path
        self.env.statik_base_asset_url = add_url_path_component(
            base_path,
            'assets',
        )
utils.py 文件源码 项目:options-screener 作者: dcwangmit01 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def render_jinja(dict_, template_str):
        """Render dict onto jinja template and return the string result"""
        name = 'jvars'
        j2env = jinja2.Environment(
            loader=jinja2.DictLoader({
                name: template_str
            }),
            undefined=jinja2.StrictUndefined,
            extensions=["jinja2.ext.do"])

        # Add some custom jinja filters
        j2env.filters['bool'] = TypeUtils.str_to_bool
        j2env.filters['yaml'] = YamlUtils.yaml_dict_to_string
        j2env.filters['base64encode'] = base64.b64encode

        # Add a "raise" keyword for raising exceptions from within jinja
        j2env.globals['raise'] = JinjaUtils._jinja_keyword_raise
        j2env.globals['gen_names'] = JinjaUtils._jinja_keyword_gen_names
        j2env.globals['mkpass'] = JinjaUtils.mkpass
        j2env.globals['keygen'] = JinjaUtils.keygen
        j2env.globals['self_signed_cert_gen'] = JinjaUtils.self_signed_cert_gen
        j2env.globals['ceph_key'] = JinjaUtils.ceph_key
        j2env.globals['uuid'] = JinjaUtils.uuid

        # Render the template
        rendered_template = j2env.get_template(name).render(dict_)
        return rendered_template + "\n"
templating.py 文件源码 项目:islam-buddy 作者: hamir 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_custom_template_loader(self):
        class MyFlask(flask.Flask):
            def create_global_jinja_loader(self):
                from jinja2 import DictLoader
                return DictLoader({'index.html': 'Hello Custom World!'})
        app = MyFlask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('index.html')
        c = app.test_client()
        rv = c.get('/')
        self.assert_equal(rv.data, b'Hello Custom World!')
templating.py 文件源码 项目:Flask-NvRay-Blog 作者: rui7157 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_custom_template_loader(self):
        class MyFlask(flask.Flask):
            def create_global_jinja_loader(self):
                from jinja2 import DictLoader
                return DictLoader({'index.html': 'Hello Custom World!'})
        app = MyFlask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('index.html')
        c = app.test_client()
        rv = c.get('/')
        self.assert_equal(rv.data, b'Hello Custom World!')
templating.py 文件源码 项目:Flask-NvRay-Blog 作者: rui7157 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_custom_template_loader(self):
        class MyFlask(flask.Flask):
            def create_global_jinja_loader(self):
                from jinja2 import DictLoader
                return DictLoader({'index.html': 'Hello Custom World!'})
        app = MyFlask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('index.html')
        c = app.test_client()
        rv = c.get('/')
        self.assert_equal(rv.data, b'Hello Custom World!')
templating.py 文件源码 项目:Callandtext 作者: iaora 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_custom_template_loader(self):
        class MyFlask(flask.Flask):
            def create_global_jinja_loader(self):
                from jinja2 import DictLoader
                return DictLoader({'index.html': 'Hello Custom World!'})
        app = MyFlask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('index.html')
        c = app.test_client()
        rv = c.get('/')
        self.assert_equal(rv.data, b'Hello Custom World!')
imports.py 文件源码 项目:Callandtext 作者: iaora 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_context_include_with_overrides(self):
        env = Environment(loader=DictLoader(dict(
            main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",
            item="{{ item }}"
        )))
        assert env.get_template("main").render() == "123"
inheritance.py 文件源码 项目:Callandtext 作者: iaora 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_super(self):
        env = Environment(loader=DictLoader({
            'a': '{% block intro %}INTRO{% endblock %}|'
                 'BEFORE|{% block data %}INNER{% endblock %}|AFTER',
            'b': '{% extends "a" %}{% block data %}({{ '
                 'super() }}){% endblock %}',
            'c': '{% extends "b" %}{% block intro %}--{{ '
                 'super() }}--{% endblock %}\n{% block data '
                 '%}[{{ super() }}]{% endblock %}'
        }))
        tmpl = env.get_template('c')
        assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'
inheritance.py 文件源码 项目:Callandtext 作者: iaora 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_preserve_blocks(self):
        env = Environment(loader=DictLoader({
            'a': '{% if false %}{% block x %}A{% endblock %}{% endif %}{{ self.x() }}',
            'b': '{% extends "a" %}{% block x %}B{{ super() }}{% endblock %}'
        }))
        tmpl = env.get_template('b')
        assert tmpl.render() == 'BA'


问题


面经


文章

微信
公众号

扫码关注公众号