添加 Template 的自动转义参数

This commit is contained in:
洛水居室 2022-06-10 17:51:28 +08:00
parent d3356af95d
commit 4cbe28015d
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -20,25 +20,25 @@ class TemplateService:
self._jinja2_env = {} self._jinja2_env = {}
self._jinja2_template = {} self._jinja2_template = {}
def get_template(self, package_path: str, template_name: str) -> Template: def get_template(self, package_path: str, template_name: str, auto_escape: bool = True) -> Template:
if config.DEBUG: if config.DEBUG:
# DEBUG下 禁止复用 方便查看和修改模板 # DEBUG下 禁止复用 方便查看和修改模板
loader = PackageLoader(self._template_package_name, package_path) loader = PackageLoader(self._template_package_name, package_path)
jinja2_env = Environment(loader=loader, enable_async=True, autoescape=True) jinja2_env = Environment(loader=loader, enable_async=True, autoescape=auto_escape)
jinja2_template = jinja2_env.get_template(template_name) jinja2_template = jinja2_env.get_template(template_name)
else: else:
jinja2_env: Environment = self._jinja2_env.get(package_path) jinja2_env: Environment = self._jinja2_env.get(package_path)
jinja2_template: Template = self._jinja2_template.get(package_path + template_name) jinja2_template: Template = self._jinja2_template.get(package_path + template_name)
if jinja2_env is None: if jinja2_env is None:
loader = PackageLoader(self._template_package_name, package_path) loader = PackageLoader(self._template_package_name, package_path)
jinja2_env = Environment(loader=loader, enable_async=True, autoescape=True) jinja2_env = Environment(loader=loader, enable_async=True, autoescape=auto_escape)
jinja2_template = jinja2_env.get_template(template_name) jinja2_template = jinja2_env.get_template(template_name)
self._jinja2_env[package_path] = jinja2_env self._jinja2_env[package_path] = jinja2_env
self._jinja2_template[package_path + template_name] = jinja2_template self._jinja2_template[package_path + template_name] = jinja2_template
return jinja2_template return jinja2_template
async def render(self, template_path: str, template_name: str, template_data: dict, async def render(self, template_path: str, template_name: str, template_data: dict,
viewport: ViewportSize, full_page: bool = True) -> bytes: viewport: ViewportSize, full_page: bool = True, auto_escape: bool = True) -> bytes:
""" """
模板渲染成图片 模板渲染成图片
:param template_path: 模板目录 :param template_path: 模板目录
@ -46,10 +46,11 @@ class TemplateService:
:param template_data: 模板数据 :param template_data: 模板数据
:param viewport: 截图大小 :param viewport: 截图大小
:param full_page: 是否长截图 :param full_page: 是否长截图
:param auto_escape: 是否自动转义
:return: :return:
""" """
start_time = time.time() start_time = time.time()
template = self.get_template(template_path, template_name) template = self.get_template(template_path, template_name, auto_escape)
template_data["res_path"] = "file://" + self._current_dir template_data["res_path"] = "file://" + self._current_dir
html = await template.render_async(**template_data) html = await template.render_async(**template_data)
Log.debug(f"{template_name} 模板渲染使用了 {str(time.time() - start_time)}") Log.debug(f"{template_name} 模板渲染使用了 {str(time.time() - start_time)}")