Ticket #1586: local_app_templates.diff
File local_app_templates.diff, 5.4 KB (added by , 17 years ago) |
---|
-
django/conf/project_template/settings.py
53 53 # List of callables that know how to import templates from various sources. 54 54 TEMPLATE_LOADERS = ( 55 55 'django.template.loaders.filesystem.load_template_source', 56 'django.template.loaders.local_app_templates.load_template_source', 56 57 'django.template.loaders.app_directories.load_template_source', 57 58 # 'django.template.loaders.eggs.load_template_source', 58 59 ) -
django/conf/global_settings.py
142 142 # documentation. 143 143 TEMPLATE_LOADERS = ( 144 144 'django.template.loaders.filesystem.load_template_source', 145 'django.template.loaders.local_app_templates.load_template_source', 145 146 'django.template.loaders.app_directories.load_template_source', 146 147 # 'django.template.loaders.eggs.load_template_source', 147 148 ) -
django/template/loaders/local_app_templates.py
1 """ 2 Wrapper for loading templates from "app_templates" directories in INSTALLED_APPS 3 packages. 4 5 Each "app_templates" directory is loaded as a virtual subdirectory matching the 6 app name for which it exists in. For example:: 7 8 myapp/app_templates/base.htm == {% extends "myapp/base.htm" %} 9 """ 10 11 import os 12 13 from django.conf import settings 14 from django.core.exceptions import ImproperlyConfigured 15 from django.template import TemplateDoesNotExist 16 from django.utils._os import safe_join 17 18 # At compile time, cache the directories to search. 19 app_template_dirs = {} 20 for 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 39 def 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 51 load_template_source.is_usable = True -
docs/settings.txt
930 930 TEMPLATE_LOADERS 931 931 ---------------- 932 932 933 Default: ``('django.template.loaders.filesystem.load_template_source',)``933 Default:: 934 934 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 935 939 A tuple of callables (as strings) that know how to import templates from 936 940 various sources. See the `template documentation`_. 937 941 -
docs/templates_python.txt
548 548 It caches a list of which ``INSTALLED_APPS`` packages have a ``templates`` 549 549 subdirectory. 550 550 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 551 577 ``django.template.loaders.eggs.load_template_source`` 552 578 Just like ``app_directories`` above, but it loads templates from Python 553 579 eggs rather than from the filesystem.