[cleanup] added frappe.utils.jinja.validate_template

This commit is contained in:
Rushabh Mehta 2016-02-08 14:50:19 +05:30
parent 7a8858e763
commit 38ac775bd6
2 changed files with 14 additions and 7 deletions

View file

@ -5,7 +5,7 @@ from __future__ import unicode_literals
import frappe
import frappe.utils
import json
from jinja2 import TemplateSyntaxError
from frappe.utils.jinja import validate_template
from frappe.model.document import Document
@ -24,12 +24,7 @@ class PrintFormat(Document):
self.extract_images()
if self.html:
jenv = frappe.get_jenv()
try:
jenv.from_string(self.html)
except TemplateSyntaxError, e:
frappe.msgprint('Line {}: {}'.format(e.lineno, e.message))
frappe.throw(frappe._("Syntax error in Jinja template"))
validate_template(self.html)
def extract_images(self):
from frappe.utils.file_manager import extract_images_from_html

View file

@ -22,6 +22,18 @@ def get_jenv():
def get_template(path):
return get_jenv().get_template(path)
def validate_template(html):
"""Throws exception if there is a syntax error in the Jinja Template"""
import frappe
from jinja2 import TemplateSyntaxError
jenv = get_jenv()
try:
jenv.from_string(html)
except TemplateSyntaxError, e:
frappe.msgprint('Line {}: {}'.format(e.lineno, e.message))
frappe.throw(frappe._("Syntax error in template"))
def render_template(template, context, is_path=None):
if is_path or template.startswith("templates/"):
return get_jenv().get_template(template).render(context)