Changeset 1372
- Timestamp:
- 11/23/05 13:05:55 (3 years ago)
- Files:
-
- django/branches/new-admin/django/core/template/defaulttags.py (modified) (1 diff)
- django/branches/new-admin/django/core/template/__init__.py (modified) (22 diffs)
- django/branches/new-admin/django/core/template/loader.py (modified) (5 diffs)
- django/branches/new-admin/django/core/template/loaders/app_directories.py (modified) (1 diff)
- django/branches/new-admin/django/core/template/loaders/eggs.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/new-admin/django/core/template/defaulttags.py
r1125 r1372 193 193 grouper = resolve_variable_with_filters('var.%s' % self.expression, \ 194 194 Context({'var': obj})) 195 # TODO: Is this a sensible way to determine equality?195 # TODO: Is this a sensible way to determine equality? 196 196 if output and repr(output[-1]['grouper']) == repr(grouper): 197 197 output[-1]['list'].append(obj) django/branches/new-admin/django/core/template/__init__.py
r1299 r1372 75 75 ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.' 76 76 77 #What to report as the origin of templates that come from non loader sources (ie strings) 77 # what to report as the origin for templates that come from non-loader sources 78 # (e.g. strings) 78 79 UNKNOWN_SOURCE="<unknown source>" 79 80 … … 108 109 def __init__(self, name): 109 110 self.name = name 110 111 111 112 def reload(self): 112 113 raise NotImplementedException … … 119 120 super(StringOrigin, self).__init__(UNKNOWN_SOURCE) 120 121 self.source = source 121 122 122 123 def reload(self): 123 124 return self.source … … 128 129 if TEMPLATE_DEBUG and origin == None: 129 130 origin = StringOrigin(template_string) 130 #Could do some crazy stack frame stuff to record where this string came from... 131 # Could do some crazy stack-frame stuff to record where this string 132 # came from... 131 133 self.nodelist = compile_string(template_string, origin) 132 134 133 135 def __iter__(self): 134 136 for node in self.nodelist: … … 141 143 142 144 def compile_string(template_string, origin): 143 "Compiles template_string into NodeList ready for rendering" 145 "Compiles template_string into NodeList ready for rendering" 144 146 lexer = lexer_factory(template_string, origin) 145 147 parser = parser_factory(lexer.tokenize()) … … 187 189 return True 188 190 return False 189 191 190 192 def get(self, key, otherwise): 191 193 for dict in self.dicts: … … 205 207 def __str__(self): 206 208 return '<%s token: "%s...">' % ( 207 {TOKEN_TEXT: 'Text', TOKEN_VAR:'Var', TOKEN_BLOCK:'Block'}[self.token_type],209 {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type], 208 210 self.contents[:20].replace('\n', '') 209 211 ) 210 212 211 213 def __repr__(self): 212 214 return '<%s token: "%s">' % ( 213 {TOKEN_TEXT: 'Text', TOKEN_VAR:'Var', TOKEN_BLOCK:'Block'}[self.token_type],215 {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type], 214 216 self.contents[:].replace('\n', '') 215 217 ) … … 219 221 self.template_string = template_string 220 222 self.origin = origin 221 223 222 224 def tokenize(self): 223 225 "Return a list of tokens from a given template_string" … … 225 227 bits = filter(None, tag_re.split(self.template_string)) 226 228 return map(self.create_token, bits) 227 229 228 230 def create_token(self,token_string): 229 231 "Convert the given token string into a new Token object and return it" … … 234 236 else: 235 237 token = Token(TOKEN_TEXT, token_string) 236 return token 238 return token 237 239 238 240 class DebugLexer(Lexer): 239 241 def __init__(self, template_string, origin): 240 super(DebugLexer, self).__init__(template_string, origin)242 super(DebugLexer, self).__init__(template_string, origin) 241 243 242 244 def tokenize(self): … … 245 247 for match in tag_re.finditer(self.template_string): 246 248 start, end = match.span() 247 if start > upto: 248 token_tups.append( (self.template_string[upto:start], (upto, start) ) )249 if start > upto: 250 token_tups.append( (self.template_string[upto:start], (upto, start)) ) 249 251 upto = start 250 token_tups.append( (self.template_string[start:end], (start,end) ) )252 token_tups.append( (self.template_string[start:end], (start,end)) ) 251 253 upto = end 252 254 last_bit = self.template_string[upto:] 253 255 if last_bit: 254 token_tups.append( (last_bit, (upto, upto + len(last_bit) )) )255 return [ self.create_token(tok, (self.origin, loc)) for tok, loc in token_tups]256 token_tups.append( (last_bit, (upto, upto + len(last_bit))) ) 257 return [self.create_token(tok, (self.origin, loc)) for tok, loc in token_tups] 256 258 257 259 def create_token(self, token_string, source): … … 289 291 compile_func = registered_tags[command] 290 292 except KeyError: 291 self.invalid_block_tag(token, command) 293 self.invalid_block_tag(token, command) 292 294 try: 293 295 compiled_result = compile_func(self, token) … … 306 308 def create_nodelist(self): 307 309 return NodeList() 308 310 309 311 def extend_nodelist(self, nodelist, node, token): 310 312 nodelist.append(node) … … 312 314 def enter_command(self, command, token): 313 315 pass 314 316 315 317 def exit_command(self): 316 318 pass … … 321 323 def empty_variable(self, token): 322 324 raise self.error( token, "Empty variable tag") 323 325 324 326 def empty_block_tag(self, token): 325 327 raise self.error( token, "Empty block tag") 326 328 327 329 def invalid_block_tag(self, token, command): 328 330 raise self.error( token, "Invalid block tag: '%s'" % command) 329 331 330 332 def unclosed_block_tag(self, parse_until): 331 333 raise self.error(None, "Unclosed tags: %s " % ', '.join(parse_until)) … … 333 335 def compile_function_error(self, token, e): 334 336 pass 335 337 336 338 def next_token(self): 337 339 return self.tokens.pop(0) … … 342 344 def delete_first_token(self): 343 345 del self.tokens[0] 344 346 345 347 class DebugParser(Parser): 346 348 def __init__(self, lexer): … … 350 352 def enter_command(self, command, token): 351 353 self.command_stack.append( (command, token.source) ) 352 354 353 355 def exit_command(self): 354 356 self.command_stack.pop() … … 371 373 node.source = token.source 372 374 super(DebugParser, self).extend_nodelist(nodelist, node, token) 373 375 374 376 def unclosed_block_tag(self, parse_until): 375 377 (command, source) = self.command_stack.pop() 376 msg = "Unclosed tag '%s'. Looking for one of: %s " % \ 377 (command, ', '.join(parse_until) ) 378 msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until)) 378 379 raise self.source_error( source, msg) 379 380 … … 662 663 'arg_sep': re.escape(FILTER_ARGUMENT_SEPARATOR), 663 664 'i18n_open' : re.escape("_("), 664 'i18n_close' : re.escape(")"), 665 'i18n_close' : re.escape(")"), 665 666 } 666 667 667 668 filter_raw_string = filter_raw_string.replace("\n", "").replace(" ", "") 668 669 filter_re = re.compile(filter_raw_string) 669 670 670 671 class RegexFilterParser(object): 671 """ Not used yet because of i18n""" 672 672 "Not used yet because of i18n" 673 673 def __init__(self, token): 674 674 matches = filter_re.finditer(token) … … 828 828 except Exception: 829 829 from sys import exc_info 830 wrapped = TemplateSyntaxError( 'Caught exception whilst rendering')830 wrapped = TemplateSyntaxError('Caught an exception while rendering.') 831 831 wrapped.source = node.source 832 832 wrapped.exc_info = exc_info() … … 859 859 else: 860 860 return output 861 861 862 862 def render(self, context): 863 863 output = resolve_variable_with_filters(self.var_string, context) django/branches/new-admin/django/core/template/loader.py
r1363 r1372 8 8 # name is the template name. 9 9 # dirs is an optional list of directories to search instead of TEMPLATE_DIRS. 10 # 10 11 # The loader should return a tuple of (template_source, path). The path returned 11 # will be shown to the user for debugging purposes, so it should identify where the template12 # was loaded from.12 # might be shown to the user for debugging purposes, so it should identify where 13 # the template was loaded from. 13 14 # 14 15 # Each loader should have an "is_usable" attribute set. This is a boolean that … … 46 47 super(LoaderOrigin, self).__init__(display_name) 47 48 self.loader, self.loadname, self.dirs = loader, name, dirs 48 49 49 50 def reload(self): 50 51 return self.loader(self.loadname, self.dirs)[0] … … 90 91 context. The template_name may be a string to load a single template using 91 92 get_template, or it may be a tuple to use select_template to find one of 92 the templates in the list. Returns a string.93 the templates in the list. Returns a string. 93 94 """ 94 95 dictionary = dictionary or {} … … 100 101 context_instance.update(dictionary) 101 102 else: 102 context_instance = Context(dictionary) 103 context_instance = Context(dictionary) 103 104 return t.render(context_instance) 104 105 … … 186 187 try: 187 188 t = get_template(template_path) 188 self. nodelist = t.nodelist189 self.template = t 189 190 except Exception, e: 190 191 if TEMPLATE_DEBUG: 191 192 raise 192 self. nodelist= None193 194 def render(self, context): 195 if self. nodelist:196 return self. nodelist.render(context)193 self.template = None 194 195 def render(self, context): 196 if self.template: 197 return self.template.render(context) 197 198 else: 198 199 return '' 199 200 200 201 class IncludeNode(Node): 201 def __init__(self, template_ path_var):202 self.template_ path_var = template_path_var202 def __init__(self, template_name): 203 self.template_name = template_name 203 204 204 205 def render(self, context): 205 206 try: 206 template_ path = resolve_variable(self.template_path_var, context)207 t = get_template(template_ path)207 template_name = resolve_variable(self.template_name, context) 208 t = get_template(template_name) 208 209 return t.render(context) 209 210 except Exception, e: django/branches/new-admin/django/core/template/loaders/app_directories.py
r1028 r1372 24 24 if os.path.isdir(template_dir): 25 25 app_template_dirs.append(template_dir) 26 26 27 27 # It won't change, so convert it to a tuple to save memory. 28 28 app_template_dirs = tuple(app_template_dirs) django/branches/new-admin/django/core/template/loaders/eggs.py
r913 r1372 19 19 for app in INSTALLED_APPS: 20 20 try: 21 return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name)) 21 return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name)) 22 22 except: 23 23 pass
