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)
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
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 = '''\ <img src="#"> <b>bold</b>''' assert result == expected
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> <img src="#"> <b>bold</b></body> </html>''' assert result == expected
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
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')
def render_template(self, text, data): with request_cached_context(clear_cache_on_start=True): return JinjaEnv.env().from_string(text).render(data)
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)
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())
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)
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)
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)
def render_template(self, text, data): return JinjaEnv.env().from_string(text).render(data)