Ticket #598: django-include-tag.diff
File django-include-tag.diff, 3.1 KB (added by , 19 years ago) |
---|
-
defaulttags.py
2 2 3 3 import sys 4 4 import template 5 import template_loader 5 6 6 7 class CommentNode(template.Node): 7 8 def render(self, context): … … 226 227 return '' # Fail silently for invalid included templates. 227 228 return output 228 229 230 231 class ConstantIncludeNode(template.Node): 232 def __init__(self, template_path): 233 try: 234 t = template_loader.get_template(template_path) 235 self.nodelist = t.nodelist 236 except Exception, e: 237 self.nodelist = None 238 239 def render(self, context): 240 if self.nodelist: 241 return self.nodelist.render(context) 242 else: 243 return '' 244 245 class IncludeNode(template.Node): 246 def __init__(self, template_path_var): 247 self.template_path_var = template_path_var 248 249 def render(self, context): 250 try: 251 template_path = template.resolve_variable(self.template_path_var, context) 252 print "IncludeNode rendering %s" % template_path 253 t = template_loader.get_template(template_path) 254 return t.render(context) 255 except Exception, e: 256 return '' # Fail silently for invalid included templates. 257 258 229 259 class LoadNode(template.Node): 230 260 def __init__(self, taglib): 231 261 self.taglib = taglib … … 607 637 raise template.TemplateSyntaxError, "Second (optional) argument to %s tag must be 'parsed'" % bits[0] 608 638 return SsiNode(bits[1], parsed) 609 639 640 def do_include(parser, token): 641 """ 642 Loads a template using standard resolution mechanisms, and renders it in the current context. 643 """ 644 bits = token.contents.split() 645 parsed = False 646 if len(bits) != 2: 647 raise template.TemplateSyntaxError, "'include' tag takes one argument: the path to the template to be included" 648 649 path = bits[1] 650 if path[0] in ('"', "'") and path[-1] == path[0]: 651 return ConstantIncludeNode(path[1:-1]) 652 return IncludeNode(bits[1]) 653 610 654 def do_load(parser, token): 611 655 """ 612 656 Load a custom template tag set. … … 622 666 # check at compile time that the module can be imported 623 667 try: 624 668 LoadNode.load_taglib(taglib) 625 except ImportError :626 raise template.TemplateSyntaxError, "'%s' is not a valid tag library " % taglib669 except ImportError, e: 670 raise template.TemplateSyntaxError, "'%s' is not a valid tag library, %s" % (taglib, e) 627 671 return LoadNode(taglib) 628 672 629 673 def do_now(parser, token): … … 762 806 template.register_tag('ifnotequal', lambda parser, token: do_ifequal(parser, token, True)) 763 807 template.register_tag('if', do_if) 764 808 template.register_tag('ifchanged', do_ifchanged) 809 template.register_tag('include', do_include) 765 810 template.register_tag('regroup', do_regroup) 766 811 template.register_tag('ssi', do_ssi) 767 812 template.register_tag('load', do_load)