SHPAML preprocess in Django template loader?

Is there a way to force the Django template loader to run all the templates that it loads (i.e. directly or via an extension / enable) via SHPAML if it shows that the HTML is out of date?

I know how to call SHPAML recursively throughout the directory, but I would prefer it to be able to run it on demand, so I don’t need to remember the HTML synchronization every time I change the SHPAML source.

I assume that using SHPAML from manage.py will work too (at least for test servers), but the ability to crack the Django template engine and make it run every file that it loads through the preprocessor will be nicer.

+3
source share
2 answers

I suspect that you can achieve what you want by inheriting django.template.loaders.app_directories.Loader(or any other bootloader) and rewriting the load_template_sourcemethod, for example:

from django.template.loaders.app_directories import Loader
from shpaml import convert_text

class SHPAMLLoader(Loader):
    def load_template_source(self, *args, **kwargs):
        shpaml_source = super(SHPAMLLoader, self).load_template_source(*args, **kwargs)
        html = convert_text(shpaml_source)
        return html

Then put your loader at the beginning of the tuple TEMPLATE_LOADERSin settings.py. Of course, you will dance SHPAML for HTML every time you load the template, so you can see some overhead. The upcoming Django 1.2 supports template caching, which can help mitigate this overhead ...

Disclaimer: This code is completely untested, sorry.

+3
source

Just created a project based on a fragment in piquadrat's answer. This is a bit more features and supports django 1.1 and 1.2 (probably 1.0).

, :)

+1

Source: https://habr.com/ru/post/1729868/


All Articles