From c24fd7d11c473287987b94ae3074b957eea01e3d Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Wed, 17 Aug 2022 19:31:00 -0400 Subject: [PATCH] template: add support for UI templates Signed-off-by: Eric Callahan --- moonraker/components/template.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/moonraker/components/template.py b/moonraker/components/template.py index 796abbf..b172a3f 100644 --- a/moonraker/components/template.py +++ b/moonraker/components/template.py @@ -25,12 +25,16 @@ class TemplateFactory: self.server = config.get_server() secrets: Secrets = self.server.load_component(config, 'secrets') self.jenv = jinja2.Environment('{%', '%}', '{', '}') - self.async_env = jinja2.Environment('{%', '%}', '{', '}', - enable_async=True) + self.async_env = jinja2.Environment( + '{%', '%}', '{', '}', enable_async=True + ) + self.ui_env = jinja2.Environment(enable_async=True) self.jenv.add_extension("jinja2.ext.do") self.jenv.filters['fromjson'] = json.loads self.async_env.add_extension("jinja2.ext.do") self.async_env.filters['fromjson'] = json.loads + self.ui_env.add_extension("jinja2.ext.do") + self.ui_env.filters['fromjson'] = json.loads self.add_environment_global('raise_error', self._raise_error) self.add_environment_global('secrets', secrets) @@ -56,6 +60,14 @@ class TemplateFactory: raise return JinjaTemplate(source, self.server, template, is_async) + def create_ui_template(self, source: str) -> JinjaTemplate: + try: + template = self.ui_env.from_string(source) + except Exception: + logging.exception(f"Error creating template from source:\n{source}") + raise + return JinjaTemplate(source, self.server, template, True) + class JinjaTemplate: def __init__(self,