[Django]-Preprocess SHPAML in Django's template loader?

3👍

I suspect you can achieve what you want by inheriting from django.template.loaders.app_directories.Loader (or whatever loader you use) and overwriting the load_template_source method, e.g.:

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 TEMPLATE_LOADERS tuple in your settings.py. Of course, you will be doing the SHPAML to HTML dance every time a template is loaded, so you may see some overhead. The upcoming Django 1.2 features template caching, which could help mitigating that overhead…

Disclaimer: this code is completely untested, sorry.

1👍

Just created a project based on the snippet in piquadrat’s answer. It’s a little more feature complete and supports django 1.1 and 1.2 (probably 1.0 as well)

Thought it might come in handy for the future 🙂

👤Jiaaro

Leave a comment