I have tried to use the Django's Template System by itself. This is the code that I have:
from django.conf import settings;
from django.template import Context, Template;
from django.template.loader import render_to_string, get_template;
settings.configure(
DEBUG = True,
TEMPLATE_DEBUG = True,
TEMPLATE_DIRS = ('.', 'base')
)
rendered = render_to_string('my_template_20.tpl', { 'my_name': 'ABC XYZ!!!' })
f = open("result_20.txt", "w");
f.write(rendered);
f.close();
The path to the file my_template_20.tpl is:
./base/my_template_20.tpl
In the file, I only have three lines:
Line First
{{my_name}}
Line Last
The text inside the file result_20.txt is:
Line First
ABC XYZ!!!
Line Last
theTemplate40 = get_template('my_parent_40.txt')
theTemplate41 = get_template('my_parent_41.txt')
ctxt40 = Context({"more_name": "Mary 40"});
ctxt41 = Context({"more_name": "Mary 41"});
rendered40 = theTemplate40.render(ctxt40);
rendered41 = theTemplate41.render(ctxt41);
f = open("base4/result_40.txt", "w");
f.write(rendered40);
f.close();
f = open("base4/result_41.txt", "w");
f.write(rendered41);
f.close();
Contents in my_parent_40.txt:
{% extends "my_template_40.tpl" %}
{% block mainFooter %}
Replacement footer from my parent 40
{% endblock %}
{% block mainContent %}
Replacement content from my parent 40
{% endblock %}
Contents in my_parent_41.txt:
{% extends "my_template_40.tpl" %}
{% block mainFooter %}
Replacement footer from my parent 41
{% endblock %}
Contents in my_template_40.tpl:
Line First template 40
{{more_name}}
Line Last template 40
{% block mainContent %}Default content from Template 40{% endblock %}
{% block mainFooter %}Default footer from Template 40{% endblock %}
Contents in result_40.txt:
Line First template 40
Mary 40
Line Last template 40
Replacement content from my parent 40
Replacement footer from my parent 40
Contents in result_41.txt:
Line First template 40
Mary 41
Line Last template 40
Default content from Template 40
Replacement footer from my parent 41