Code

Ticket #1169: django-core-template-loader.py.diff

File django-core-template-loader.py.diff, 1.2 KB (added by eric@…, 9 years ago)

Patch to render a template to file on disk

Line 
1Index: django/core/template/loader.py
2===================================================================
3--- django/core/template/loader.py      (revision 1827)
4+++ django/core/template/loader.py      (working copy)
5@@ -97,6 +97,20 @@
6         context_instance = Context(dictionary)
7     return t.render(context_instance)
8 
9+def render_to_file(template_name, file_path, mode='w', dictionary=None, context_instance=None):
10+    """
11+    Loads the given template_name and renders it, with the given dictionary as
12+    context, to a file on disk.  The template_name may be a string to load a
13+    single template using get_template, or it may be a tuple to use
14+    select_template to find one of the templates in the list.  file_path defines
15+    the file that will be written to disk.  mode defaults to 'w' which will
16+    create the file if it doesn't exist and will overwrite the file if it does
17+    exist.
18+    """
19+    f = open(file_path, mode)
20+    f.write(render_to_string(template_name, dictionary, context_instance))
21+    f.close()
22+
23 def select_template(template_name_list):
24     "Given a list of template names, returns the first that can be loaded."
25     for template_name in template_name_list: