Override django include template tag to read the html from database

191 Views Asked by At

I'm trying to write my own custom Django include template tag, to read the text to render from database, instead of reading a HTML file from disk. Something like this:

{% dbinclude 'template_id_from_database' with foo='bar' %}

A query would be raised to get some HTML text containing variables from database, and those variables would be replaced with their values, passed vía with keyword.

That would be my approach, but reading the documentation, looks like there may be an easier way. Related with the variable passed to include tag, it says that:

The variable may also be any object with a render() method that accepts a context. This allows you to reference a compiled Template in your context.

So, instead of writing my own include template tag, I could just pass an instance of a Template object (or a subclass) as argument. In that instance, I could read the text to render from database. Still don't know how to do that, but the concept is here.

So, the question is: what of these approaches would be more Django-good-practices-friendly, if any?

0

There are 0 best solutions below