Ticket #6834: loader_tags.diff
File loader_tags.diff, 5.2 KB (added by , 17 years ago) |
---|
-
django/template/loader_tags.py
39 39 class ExtendsNode(Node): 40 40 must_be_first = True 41 41 42 def __init__(self, nodelist, parent_name, parent_name_expr, template_dirs=None): 42 def __init__(self, nodelist, parent_name, parent_name_expr, 43 template_dirs_expr=None): 43 44 self.nodelist = nodelist 44 45 self.parent_name, self.parent_name_expr = parent_name, parent_name_expr 45 self.template_dirs = template_dirs46 self.template_dirs_expr = template_dirs_expr 46 47 47 48 def __repr__(self): 48 49 if self.parent_name_expr: … … 53 54 if self.parent_name_expr: 54 55 self.parent_name = self.parent_name_expr.resolve(context) 55 56 parent = self.parent_name 57 template_dirs = None 58 if self.template_dirs_expr: 59 template_dirs = self.template_dirs_expr.resolve(context) 56 60 if not parent: 57 61 error_msg = "Invalid template name in 'extends' tag: %r." % parent 58 62 if self.parent_name_expr: … … 61 65 if hasattr(parent, 'render'): 62 66 return parent # parent is a Template object 63 67 try: 64 source, origin = find_template_source(parent, self.template_dirs)68 source, origin = find_template_source(parent, template_dirs) 65 69 except TemplateDoesNotExist: 66 70 raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent 67 71 else: … … 93 97 return compiled_parent.render(context) 94 98 95 99 class ConstantIncludeNode(Node): 96 def __init__(self, template_path ):100 def __init__(self, template_path, dirs=None): 97 101 try: 98 t = get_template(template_path )102 t = get_template(template_path, dirs) 99 103 self.template = t 100 104 except: 101 105 if settings.TEMPLATE_DEBUG: … … 109 113 return '' 110 114 111 115 class IncludeNode(Node): 112 def __init__(self, template_name ):116 def __init__(self, template_name, dirs=None): 113 117 self.template_name = Variable(template_name) 118 self.dirs = dirs 114 119 115 120 def render(self, context): 116 121 try: 117 122 template_name = self.template_name.resolve(context) 118 t = get_template(template_name )123 t = get_template(template_name, dirs) 119 124 return t.render(context) 120 125 except TemplateSyntaxError, e: 121 126 if settings.TEMPLATE_DEBUG: … … 153 158 or ``{% extends variable %}`` uses the value of ``variable`` as either the 154 159 name of the parent template to extend (if it evaluates to a string) or as 155 160 the parent tempate itelf (if it evaluates to a Template object). 161 162 This tag accepts an optional second argument which, if provided and not a 163 variable which is None, will override the settings.TEMPLATE_DIRS value. 164 If provided, it should be a variable which is a list of directories. 156 165 """ 157 166 bits = token.contents.split() 158 if len(bits) != 2 :159 raise TemplateSyntaxError, "'%s' takes one argument" % bits[0]167 if len(bits) != 2 and len(bits) != 3: 168 raise TemplateSyntaxError, "'%s' takes one or two arguments" % bits[0] 160 169 parent_name, parent_name_expr = None, None 170 dirs_expr = None 161 171 if bits[1][0] in ('"', "'") and bits[1][-1] == bits[1][0]: 162 172 parent_name = bits[1][1:-1] 163 173 else: 164 174 parent_name_expr = parser.compile_filter(bits[1]) 175 if len(bits) == 3: 176 dirs_expr = parser.compile_filter(bits[2]) 165 177 nodelist = parser.parse() 166 178 if nodelist.get_nodes_by_type(ExtendsNode): 167 179 raise TemplateSyntaxError, "'%s' cannot appear more than once in the same template" % bits[0] 168 return ExtendsNode(nodelist, parent_name, parent_name_expr )180 return ExtendsNode(nodelist, parent_name, parent_name_expr, dirs_expr) 169 181 170 182 def do_include(parser, token): 171 183 """ … … 176 188 {% include "foo/some_include" %} 177 189 """ 178 190 bits = token.contents.split() 179 if len(bits) != 2 :191 if len(bits) != 2 and len(bits) != 3: 180 192 raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0] 181 193 path = bits[1] 194 dirs_expr = None 195 if len(bits) == 3: 196 dirs_expr = parser.compile_filter(bits[2]) 182 197 if path[0] in ('"', "'") and path[-1] == path[0]: 183 return ConstantIncludeNode(path[1:-1] )184 return IncludeNode(bits[1] )198 return ConstantIncludeNode(path[1:-1], dirs_expr) 199 return IncludeNode(bits[1], dirs_expr) 185 200 186 201 register.tag('block', do_block) 187 202 register.tag('extends', do_extends) -
docs/templates.txt
646 646 parent template. If the variable evaluates to a ``Template`` object, 647 647 Django will use that object as the parent template. 648 648 649 ``{% extends "name" template_paths %}`` works for either of the above two ways, 650 but will use the variable ``template_paths`` to provide the list of directories 651 in which to search for templates. 652 649 653 See `Template inheritance`_ for more information. 650 654 651 655 filter