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

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

Patch to render a template to file on disk

  • django/core/template/loader.py

    9797        context_instance = Context(dictionary)
    9898    return t.render(context_instance)
     100def render_to_file(template_name, file_path, mode='w', dictionary=None, context_instance=None):
     101    """
     102    Loads the given template_name and renders it, with the given dictionary as
     103    context, to a file on disk.  The template_name may be a string to load a
     104    single template using get_template, or it may be a tuple to use
     105    select_template to find one of the templates in the list.  file_path defines
     106    the file that will be written to disk.  mode defaults to 'w' which will
     107    create the file if it doesn't exist and will overwrite the file if it does
     108    exist.
     109    """
     110    f = open(file_path, mode)
     111    f.write(render_to_string(template_name, dictionary, context_instance))
     112    f.close()
    100114def select_template(template_name_list):
    101115    "Given a list of template names, returns the first that can be loaded."
    102116    for template_name in template_name_list:
Back to Top