Cloned DMOJ
This commit is contained in:
parent
f623974b58
commit
49dc9ff10c
513 changed files with 132349 additions and 39 deletions
27
judge/jinja2/render.py
Normal file
27
judge/jinja2/render.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
from django.template import (Context, Template as DjangoTemplate, TemplateSyntaxError as DjangoTemplateSyntaxError,
|
||||
VariableDoesNotExist)
|
||||
|
||||
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):
|
||||
return 'Error rendering: %r' % template
|
Loading…
Add table
Add a link
Reference in a new issue