File: template.py

package info (click to toggle)
python-django-parler 2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,012 kB
  • sloc: python: 4,291; makefile: 164; sh: 6
file content (28 lines) | stat: -rw-r--r-- 976 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from django.template import TemplateDoesNotExist
from django.template.loader import get_template

_cached_name_lookups = {}


def select_template_name(template_name_list, using=None):
    """
    Given a list of template names, find the first one that exists.
    """
    if not isinstance(template_name_list, tuple):
        template_name_list = tuple(template_name_list)

    try:
        return _cached_name_lookups[template_name_list]
    except KeyError:
        # Find which template of the template_names is selected by the Django loader.
        for template_name in template_name_list:
            try:
                get_template(template_name, using=using)
            except TemplateDoesNotExist:
                continue
            else:
                template_name = str(template_name)  # consistent value for lazy() function.
                _cached_name_lookups[template_name_list] = template_name
                return template_name

        return None