Ticket #13984: 0002-template_name-as-optional-return-value-of-django.temp.diff

File 0002-template_name-as-optional-return-value-of-django.temp.diff, 1.8 KB (added by Josh Stegmaier, 14 years ago)

Adds option to return template name with select_template and documents change.

  • django/template/loader.py

     
    185185        context_instance = Context(dictionary)
    186186    return t.render(context_instance)
    187187
    188 def select_template(template_name_list):
     188def select_template(template_name_list, return_template=False):
    189189    "Given a list of template names, returns the first that can be loaded."
    190190    for template_name in template_name_list:
    191191        try:
    192             return get_template(template_name)
     192            if return_template:
     193                return get_template(template_name), template_name
     194            else:
     195                return get_template(template_name)
    193196        except TemplateDoesNotExist:
    194197            continue
    195198    # If we get here, none of the templates could be loaded
  • docs/ref/templates/api.txt

     
    525525    the template with the given name. If the template doesn't exist, it raises
    526526    ``django.template.TemplateDoesNotExist``.
    527527
    528 .. function:: django.template.loader.select_template(template_name_list)
     528.. function:: django.template.loader.select_template(template_name_list, return_name=False)
    529529
    530530    ``select_template`` is just like ``get_template``, except it takes a list
    531531    of template names. Of the list, it returns the first template that exists.
     532    If the optional argument return_name is set to True, it will also return
     533    the selected templates name.
    532534
    533535For example, if you call ``get_template('story_detail.html')`` and have the
    534536above :setting:`TEMPLATE_DIRS` setting, here are the files Django will look for,
Back to Top