NDOJ/judge/jinja2/render.py

32 lines
745 B
Python
Raw Normal View History

2022-05-14 17:57:27 +00:00
from django.template import (
Context,
Template as DjangoTemplate,
TemplateSyntaxError as DjangoTemplateSyntaxError,
VariableDoesNotExist,
)
2020-01-21 06:35:58 +00:00
from . import registry
MAX_CACHE = 100
django_cache = {}
def compile_template(code):
if code in django_cache:
return django_cache[code]
# If this works for re.compile, it works for us too.
if len(django_cache) > MAX_CACHE:
django_cache.clear()
t = django_cache[code] = DjangoTemplate(code)
return t
@registry.function
def render_django(template, **context):
try:
return compile_template(template).render(Context(context))
except (VariableDoesNotExist, DjangoTemplateSyntaxError):
2022-05-14 17:57:27 +00:00
return "Error rendering: %r" % template