1
Fork 0

Remove jinja2

Performance makes little difference, and I have block tags now
This commit is contained in:
Jake Howard 2024-06-18 17:38:57 +01:00
parent 633d788cd6
commit 2ba54c9a90
Signed by: jake
GPG key ID: 57AFB45680EDD477
7 changed files with 63 additions and 17 deletions

View file

@ -4,4 +4,3 @@ gunicorn
markdown
chrono
django-debug-toolbar
django-jinja

View file

@ -1,8 +1,7 @@
from django.db import models
import markdown
from django.utils.functional import cached_property
from django.template import engines
from django_jinja.backend import Origin
from django.template import Template
class Tag(models.Model):
__yamdl__ = True
@ -51,7 +50,5 @@ class Page(models.Model):
@cached_property
def content_template(self):
if (cached_template := self._template_cache.get(self.slug)) is None:
cached_template = self._template_cache[self.slug] = engines["jinja2"].from_string(self.content)
cached_template.origin = Origin(name=self.slug, template_name=self.slug)
cached_template.name = self.slug
cached_template = self._template_cache[self.slug] = Template(self.content, name=self.slug)
return cached_template

View file

@ -0,0 +1,48 @@
from inspect import getfullargspec, unwrap
from django.template.library import parse_bits, SimpleNode
from functools import wraps
class SimpleBlockNode(SimpleNode):
def __init__(self, nodelist, func, takes_context, args, kwargs, target_var):
super().__init__(func, takes_context, args, kwargs, target_var)
self.nodelist = nodelist
def get_resolved_arguments(self, context):
resolved_args, resolved_kwargs = super().get_resolved_arguments(context)
resolved_args.insert(1 if self.takes_context else 0, self.nodelist.render(context))
return resolved_args, resolved_kwargs
def simple_block_tag(register, takes_context=None, name=None):
def dec(func):
argspec = getfullargspec(unwrap(func))
function_name = name or func.__name__
@wraps(func)
def tag_compiler(parser, token):
bits = token.split_contents()[1:]
target_var = None
if len(bits) >= 2 and bits[-2] == "as":
target_var = bits[-1]
bits = bits[:-2]
bits = [''] + bits # add placeholder for content arg
args, kwargs = parse_bits(
parser=parser,
bits=bits,
params=argspec.args,
varargs=argspec.varargs,
varkw=argspec.varkw,
defaults=argspec.defaults,
kwonly=argspec.kwonlyargs,
kwonly_defaults=argspec.kwonlydefaults,
takes_context=takes_context,
name=function_name
)
args = args[1:] # remove content placeholder
nodelist = parser.parse((f'end{function_name}',))
parser.delete_first_token()
return SimpleBlockNode(nodelist, func, takes_context, args, kwargs, target_var)
register.tag(function_name, tag_compiler)
return func
return dec

View file

@ -1,7 +1,9 @@
{% load core_tags %}
<head>
<title>{{ page.title }}</title>
</head>
<body>
{% mytag %}content{% endmytag %}
{{ page.toc.html|safe }}
{{ content|safe }}
{{ content }}
</body>

View file

@ -0,0 +1,9 @@
from yamdl_playground.core.simple_tags import simple_block_tag
from django import template
register = template.Library()
@simple_block_tag(register, takes_context=True)
def mytag(context, content):
return "foo{}bar".format(content)

View file

@ -19,7 +19,7 @@ def content(request, slug):
return render(request, "content.html", {
"page": page,
"content": page.content_template.render({"request": request, "page": page})
"content": page.content_template.render(Context({"request": request, "page": page}))
})
cached_content = cache_page(600, cache="mem")(content)

View file

@ -58,15 +58,6 @@ TEMPLATES = [
],
},
},
{
'BACKEND': 'django_jinja.jinja2.Jinja2',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'app_dirname': "jinja2",
"match_extension": ".html"
}
},
]
WSGI_APPLICATION = 'yamdl_playground.wsgi.application'