Add render_template fallback

This commit is contained in:
Matthias 2020-03-01 09:30:30 +01:00
parent a030ce9348
commit eda77aeec8
1 changed files with 12 additions and 0 deletions

View File

@ -148,3 +148,15 @@ def render_template(templatefile: str, arguments: dict = {}) -> str:
)
template = env.get_template(templatefile)
return template.render(**arguments)
def render_template_with_fallback(templatefile: str, templatefallbackfile: str,
arguments: dict = {}) -> str:
"""
Use templatefile if possible, otherwise fall back to templatefallbackfile
"""
from jinja2.exceptions import TemplateNotFound
try:
return render_template(templatefile, arguments)
except TemplateNotFound:
return render_template(templatefallbackfile, arguments)