~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/template/loader.py

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2014-09-17 14:15:11 UTC
  • mfrom: (1.3.17) (6.2.18 experimental)
  • Revision ID: package-import@ubuntu.com-20140917141511-icneokthe9ww5sk4
Tags: 1.7-2
* Release to unstable.
* Add a migrate-south sample script to help users apply their South
  migrations. Thanks to Brian May.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
# installed, because pkg_resources is necessary to read eggs.
27
27
 
28
28
from django.core.exceptions import ImproperlyConfigured
29
 
from django.template.base import Origin, Template, Context, TemplateDoesNotExist, add_to_builtins
 
29
from django.template.base import Origin, Template, Context, TemplateDoesNotExist
30
30
from django.conf import settings
31
 
from django.utils.module_loading import import_by_path
 
31
from django.utils.module_loading import import_string
32
32
from django.utils import six
33
33
 
34
34
template_source_loaders = None
35
35
 
 
36
 
36
37
class BaseLoader(object):
37
38
    is_usable = False
38
39
 
61
62
        name.
62
63
 
63
64
        """
64
 
        raise NotImplementedError
 
65
        raise NotImplementedError('subclasses of BaseLoader must provide a load_template_source() method')
65
66
 
66
67
    def reset(self):
67
68
        """
71
72
        """
72
73
        pass
73
74
 
 
75
 
74
76
class LoaderOrigin(Origin):
75
77
    def __init__(self, display_name, loader, name, dirs):
76
78
        super(LoaderOrigin, self).__init__(display_name)
79
81
    def reload(self):
80
82
        return self.loader(self.loadname, self.dirs)[0]
81
83
 
 
84
 
82
85
def make_origin(display_name, loader, name, dirs):
83
86
    if settings.TEMPLATE_DEBUG and display_name:
84
87
        return LoaderOrigin(display_name, loader, name, dirs)
85
88
    else:
86
89
        return None
87
90
 
 
91
 
88
92
def find_template_loader(loader):
89
93
    if isinstance(loader, (tuple, list)):
90
94
        loader, args = loader[0], loader[1:]
91
95
    else:
92
96
        args = []
93
97
    if isinstance(loader, six.string_types):
94
 
        TemplateLoader = import_by_path(loader)
 
98
        TemplateLoader = import_string(loader)
95
99
 
96
100
        if hasattr(TemplateLoader, 'load_template_source'):
97
101
            func = TemplateLoader(*args)
110
114
    else:
111
115
        raise ImproperlyConfigured('Loader does not define a "load_template" callable template source loader')
112
116
 
 
117
 
113
118
def find_template(name, dirs=None):
114
119
    # Calculate template_source_loaders the first time the function is executed
115
120
    # because putting this logic in the module-level namespace may cause
130
135
            pass
131
136
    raise TemplateDoesNotExist(name)
132
137
 
133
 
def get_template(template_name):
 
138
 
 
139
def get_template(template_name, dirs=None):
134
140
    """
135
141
    Returns a compiled Template object for the given template name,
136
142
    handling template inheritance recursively.
137
143
    """
138
 
    template, origin = find_template(template_name)
 
144
    template, origin = find_template(template_name, dirs)
139
145
    if not hasattr(template, 'render'):
140
146
        # template needs to be compiled
141
147
        template = get_template_from_string(template, origin, template_name)
142
148
    return template
143
149
 
 
150
 
144
151
def get_template_from_string(source, origin=None, name=None):
145
152
    """
146
153
    Returns a compiled Template object for the given template code,
148
155
    """
149
156
    return Template(source, origin, name)
150
157
 
151
 
def render_to_string(template_name, dictionary=None, context_instance=None):
 
158
 
 
159
def render_to_string(template_name, dictionary=None, context_instance=None,
 
160
                     dirs=None):
152
161
    """
153
162
    Loads the given template_name and renders it with the given dictionary as
154
163
    context. The template_name may be a string to load a single template using
155
164
    get_template, or it may be a tuple to use select_template to find one of
156
165
    the templates in the list. Returns a string.
157
166
    """
158
 
    dictionary = dictionary or {}
159
167
    if isinstance(template_name, (list, tuple)):
160
 
        t = select_template(template_name)
 
168
        t = select_template(template_name, dirs)
161
169
    else:
162
 
        t = get_template(template_name)
 
170
        t = get_template(template_name, dirs)
163
171
    if not context_instance:
164
172
        return t.render(Context(dictionary))
 
173
    if not dictionary:
 
174
        return t.render(context_instance)
165
175
    # Add the dictionary to the context stack, ensuring it gets removed again
166
176
    # to keep the context_instance in the same state it started in.
167
 
    context_instance.update(dictionary)
168
 
    try:
 
177
    with context_instance.push(dictionary):
169
178
        return t.render(context_instance)
170
 
    finally:
171
 
        context_instance.pop()
172
 
 
173
 
def select_template(template_name_list):
 
179
 
 
180
 
 
181
def select_template(template_name_list, dirs=None):
174
182
    "Given a list of template names, returns the first that can be loaded."
175
183
    if not template_name_list:
176
184
        raise TemplateDoesNotExist("No template names provided")
177
185
    not_found = []
178
186
    for template_name in template_name_list:
179
187
        try:
180
 
            return get_template(template_name)
 
188
            return get_template(template_name, dirs)
181
189
        except TemplateDoesNotExist as e:
182
190
            if e.args[0] not in not_found:
183
191
                not_found.append(e.args[0])
184
192
            continue
185
193
    # If we get here, none of the templates could be loaded
186
194
    raise TemplateDoesNotExist(', '.join(not_found))
187
 
 
188
 
add_to_builtins('django.template.loader_tags')