Business:
Practical background for all that is design of web-app, where web-page constructed from prototype objects, gadgets, which individually defined each in its own view function (its own code, not necessarily django-view function). And to build a ‘production’ web-page they can be placed in regular view, related to this page, and produce web-page elements according to given source-data and other parameters.
For example I have a chart-gadget, and it receives source data, parameters, about chart-type, beauty/colors etc, and draws a chart. How can it be stored in separate from specific ap, loose-coupled code? Which design-approach is better?
What Ive tried:
Ive tried ‘dumb’ way:
For example I have a simple view:
from django.shortcuts import render_to_response
from django.template import Template, Context
from string import letters
def home(request):
t = Template("""<ul>
{% for key, value in d.items %}
<li>{{ key }}: {{ value }}</li>
{% endfor %}
</ul>""")
d = {k: v for v, k in enumerate(letters)}
return render_to_response('home.html', {'t': t, 'd':d})
And template:
<html>
<head>
<title>Django tests</title>
</head>
<body>
{% include t with d=d %}
</body>
</html>
With such setup I get: TemplateDoesNotExist
Expectations about answer:
Im searching any reasonable scheme for storing chunk of web-page, which designed to kinda live by its own life, less-or-more separately from other elements of web-page, in a similarly separate chunk of code in web-app program backend.
As example I can provide such projects as:
Thank you!
ps
Extracts from django-docs:
{% extends variable %} uses the value of variable. If the variable
evaluates to a string, Django will use that string as the name of the
parent template. If the variable evaluates to a Template object,
Django will use that object as the parent template.
So It means, that with {% extends %} tag, operation what Im talking in question is definitely possible.
This example includes the contents of the template whose name is
contained in the variable template_name:
{% include template_name %}
So it probably means, that variable passed to {% include %} can bu just a string with name of file. And if its true, it is clear answer on my question – template defined in variable cannot be included in regular template.
It still slightly ambiguous for me, because in Python word name might be used as synonym of variable.
As someone already mentioned in a comment, you need to just render your template to a string and pass it in to the second template, marking it as a safe string so it isn’t auto-escaped. So, your example would be:
Then, in home.html, render with
{{ t }}