Ticket #1586: local_app_templates.diff

File local_app_templates.diff, 5.4 KB (added by Chris Beaven, 16 years ago)

Just for kicks, I made a similar template loader to this (without even checking for an existing ticket) -- thought I may as well put it up here...

  • django/conf/project_template/settings.py

     
    5353# List of callables that know how to import templates from various sources.
    5454TEMPLATE_LOADERS = (
    5555    'django.template.loaders.filesystem.load_template_source',
     56    'django.template.loaders.local_app_templates.load_template_source',
    5657    'django.template.loaders.app_directories.load_template_source',
    5758#     'django.template.loaders.eggs.load_template_source',
    5859)
  • django/conf/global_settings.py

     
    142142# documentation.
    143143TEMPLATE_LOADERS = (
    144144    'django.template.loaders.filesystem.load_template_source',
     145    'django.template.loaders.local_app_templates.load_template_source',
    145146    'django.template.loaders.app_directories.load_template_source',
    146147#     'django.template.loaders.eggs.load_template_source',
    147148)
  • django/template/loaders/local_app_templates.py

     
     1"""
     2Wrapper for loading templates from "app_templates" directories in INSTALLED_APPS
     3packages.
     4
     5Each "app_templates" directory is loaded as a virtual subdirectory matching the
     6app name for which it exists in. For example::
     7
     8    myapp/app_templates/base.htm == {% extends "myapp/base.htm" %}
     9"""
     10
     11import os
     12
     13from django.conf import settings
     14from django.core.exceptions import ImproperlyConfigured
     15from django.template import TemplateDoesNotExist
     16from django.utils._os import safe_join
     17
     18# At compile time, cache the directories to search.
     19app_template_dirs = {}
     20for app in settings.INSTALLED_APPS:
     21    i = app.rfind('.')
     22    if i == -1:
     23        m, a = app, None
     24    else:
     25        m, a = app[:i], app[i+1:]
     26    try:
     27        if a is None:
     28            mod = __import__(m, {}, {}, [])
     29        else:
     30            mod = getattr(__import__(m, {}, {}, [a]), a)
     31    except ImportError, e:
     32        raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0])
     33    app_dir = os.path.dirname(mod.__file__)
     34    template_dir = os.path.join(app_dir, 'app_templates')
     35    print template_dir
     36    if os.path.isdir(template_dir):
     37        app_template_dirs[os.path.basename(app_dir)] = template_dir
     38
     39def load_template_source(template_name, template_dirs=None):
     40    template_bits = os.path.normpath(template_name).split(os.sep, 1)
     41    if len(template_bits) == 2:
     42        app_name, template_partname = template_bits
     43        if app_name in app_template_dirs:
     44            filepath = safe_join(app_template_dirs[app_name], template_partname)
     45            try:
     46                return (open(filepath).read().decode(settings.FILE_CHARSET),
     47                        filepath)
     48            except IOError:
     49                pass
     50    raise TemplateDoesNotExist, template_name
     51load_template_source.is_usable = True
  • docs/settings.txt

     
    930930TEMPLATE_LOADERS
    931931----------------
    932932
    933 Default: ``('django.template.loaders.filesystem.load_template_source',)``
     933Default::
    934934
     935    ('django.template.loaders.filesystem.load_template_source',
     936     'django.template.loaders.local_app_templates.load_template_source',
     937     'django.template.loaders.app_directories.load_template_source',)
     938
    935939A tuple of callables (as strings) that know how to import templates from
    936940various sources. See the `template documentation`_.
    937941
  • docs/templates_python.txt

     
    548548    It caches a list of which ``INSTALLED_APPS`` packages have a ``templates``
    549549    subdirectory.
    550550
     551``django.template.loaders.local_app_templates.load_template_source``
     552    Loads templates from Django apps on the filesystem. For each app in
     553    ``INSTALLED_APPS``, the loader looks for an ``app_templates`` subdirectory.
     554    If the directory exists, Django will look for templates starting with a
     555    directory matching the app name in there.
     556
     557    For example, for this setting::
     558
     559        INSTALLED_APPS = ('myproject.polls', 'myproject.music')
     560
     561    ...then ``get_template('polls/foo.html')`` will look for templates in:
     562
     563        * ``/path/to/myproject/polls/app_templates/foo.html``
     564
     565    ...or ``get_template('music/foo.html')`` will look for templates in:
     566
     567        * ``/path/to/myproject/music/app_templates/foo.html``
     568
     569    This loader has the same distribution advantages of ``app_directories`` but
     570    is more efficient, since it only checks for templates in the application
     571    directory.
     572
     573    Note that like to the ``app_directories`` loader, this loader performs a
     574    similar optimization when it is first imported, caching a list of which
     575    ``INSTALLED_APPS`` packages have an ``app_templates`` subdirectory.
     576
    551577``django.template.loaders.eggs.load_template_source``
    552578    Just like ``app_directories`` above, but it loads templates from Python
    553579    eggs rather than from the filesystem.
Back to Top