Ticket #598: django-include-tag.diff

File django-include-tag.diff, 3.1 KB (added by robert@…, 10 years ago)

The patch

  • defaulttags.py

     
    22
    33import sys
    44import template
     5import template_loader
    56
    67class CommentNode(template.Node):
    78    def render(self, context):
     
    226227                return '' # Fail silently for invalid included templates.
    227228        return output
    228229
     230
     231class 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
     245class 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
    229259class LoadNode(template.Node):
    230260    def __init__(self, taglib):
    231261        self.taglib = taglib
     
    607637            raise template.TemplateSyntaxError, "Second (optional) argument to %s tag must be 'parsed'" % bits[0]
    608638    return SsiNode(bits[1], parsed)
    609639
     640def 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
    610654def do_load(parser, token):
    611655    """
    612656    Load a custom template tag set.
     
    622666    # check at compile time that the module can be imported
    623667    try:
    624668        LoadNode.load_taglib(taglib)
    625     except ImportError:
    626         raise template.TemplateSyntaxError, "'%s' is not a valid tag library" % taglib
     669    except ImportError, e:
     670        raise template.TemplateSyntaxError, "'%s' is not a valid tag library, %s" % (taglib, e)
    627671    return LoadNode(taglib)
    628672
    629673def do_now(parser, token):
     
    762806template.register_tag('ifnotequal', lambda parser, token: do_ifequal(parser, token, True))
    763807template.register_tag('if', do_if)
    764808template.register_tag('ifchanged', do_ifchanged)
     809template.register_tag('include', do_include)
    765810template.register_tag('regroup', do_regroup)
    766811template.register_tag('ssi', do_ssi)
    767812template.register_tag('load', do_load)
Back to Top