예제 #1
0
 def test_timedelta_filter_in_template(self):
     dt = datetime.utcnow()
     env = JinjaEnv.env()
     template = env.from_string(
         '{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = (dt + timedelta(days=-5)).strftime("%A, %B %-e")
     assert expected == template.render(dt=dt)
예제 #2
0
def render(template_name_list, data=None, encoding='utf-8'):
    data = renderable_data(data)
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list)
    rendered = template.render(data)
    if encoding:
        return rendered.encode(encoding)
    return rendered
예제 #3
0
def render(template_name_list, data=None, encoding='utf-8'):
    data = renderable_data(data)
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list)
    rendered = template.render(data)
    if encoding:
        return rendered.encode(encoding)
    return rendered
예제 #4
0
def test_verify_jinja_autoescape_string():
    env = JinjaEnv.env()
    template = env.from_string('''\
{{ unsafe_string }}
{% autoescape false %}{{ safe_string }}{% endautoescape %}''')

    result = template.render(unsafe_string='<img src="#">', safe_string='<b>bold</b>')
    expected = '''\
&lt;img src=&#34;#&#34;&gt;
<b>bold</b>'''

    assert result == expected
예제 #5
0
def test_verify_jinja_autoescape_template():
    env = JinjaEnv.env()
    cur_dir = os.path.dirname(__file__)
    env.loader.searchpath.append(cur_dir)
    try:
        template = env.get_template('autoescape_template.html')
    finally:
        if cur_dir in env.loader.searchpath:
            env.loader.searchpath.remove(cur_dir)

    result = template.render(unsafe_string='<img src="#">', safe_string='<b>bold</b>')
    expected = '''\
<!DOCTYPE HTML>
<html>
<body>
&lt;img src=&#34;#&#34;&gt;
<b>bold</b></body>
</html>'''

    assert result == expected
예제 #6
0
def test_render_empty():
    env = JinjaEnv.env()
    cur_dir = os.path.dirname(__file__)
    env.loader.searchpath.append(cur_dir)
    try:
        template = env.get_template('autoescape_template.html')
    finally:
        if cur_dir in env.loader.searchpath:
            env.loader.searchpath.remove(cur_dir)

    result = str(open(template.filename, 'r').read())
    expected = '''\
<!DOCTYPE HTML>
<html>
<body>
{{ unsafe_string }}
{% autoescape false %}{{ safe_string }}{% endautoescape %}
</body>
</html>
'''

    assert result == expected
예제 #7
0
def render_empty(template_name_list):
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list, use_request_cache=False)
    return open(template.filename, 'rb').read().decode('utf-8')
예제 #8
0
 def render_template(self, text, data):
     with request_cached_context(clear_cache_on_start=True):
         return JinjaEnv.env().from_string(text).render(data)
예제 #9
0
 def test_timedelta_filter_in_template_with_empty_date(self):
     env = JinjaEnv.env()
     template = env.from_string(
         '{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = ''
     assert expected == template.render(dt=None)
예제 #10
0
def render_empty(template_name_list):
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list)
    return str(open(template.filename, 'r').read())
예제 #11
0
def is_valid_jinja_template(template_path):
    env = JinjaEnv.env()
    with open(template_path) as template_file:
        template = template_file.read()
        ast = env.parse(template)
        meta.find_undeclared_variables(ast)
예제 #12
0
def render_empty(template_name_list):
    env = JinjaEnv.env()
    template = env.get_or_select_template(template_name_list, use_request_cache=False)
    return open(template.filename, 'rb').read().decode('utf-8')
예제 #13
0
def is_valid_jinja_template(template_path):
    env = JinjaEnv.env()
    with open(template_path) as template_file:
        template = template_file.read()
        ast = env.parse(template)
        meta.find_undeclared_variables(ast)
예제 #14
0
 def test_timedelta_filter_in_template_with_empty_date(self):
     env = JinjaEnv.env()
     template = env.from_string('{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = ''
     assert expected == template.render(dt=None)
예제 #15
0
 def test_timedelta_filter_in_template(self):
     dt = datetime.utcnow()
     env = JinjaEnv.env()
     template = env.from_string('{{ dt|timedelta(days=-5)|datetime("%A, %B %-e") }}')
     expected = (dt + timedelta(days=-5)).strftime("%A, %B %-e")
     assert expected == template.render(dt=dt)
예제 #16
0
 def render_template(self, text, data):
     return JinjaEnv.env().from_string(text).render(data)