def tryload(parser, token): bits = token.contents.split() if len(bits) >= 4 and bits[-2] == "from": try: taglib = bits[-1] lib = get_library(taglib) except InvalidTemplateLibrary as e: return LoadNode() else: temp_lib = Library() for name in bits[1:-2]: if name in lib.tags: temp_lib.tags[name] = lib.tags[name] if name in lib.filters: temp_lib.filters[name] = lib.filters[name] elif name in lib.filters: temp_lib.filters[name] = lib.filters[name] else: return LoadNode() parser.add_library(temp_lib) else: for taglib in bits[1:]: try: lib = get_library(taglib) parser.add_library(lib) except InvalidTemplateLibrary as e: return LoadNode() return LoadNode()
def friendly_load(parser, token): """ Tries to load a custom template tag set. Non existing tag libraries are ignored. This means that, if used in conjuction with ``if_has_tag``, you can try to load the comments template tag library to enable comments even if the comments framework is not installed. For example:: {% load friendly_loader %} {% friendly_load comments webdesign %} {% if_has_tag render_comment_list %} {% render_comment_list for obj %} {% else %} {% if_has_tag lorem %} {% lorem %} {% endif_has_tag %} {% endif_has_tag %} """ bits = token.contents.split() for taglib in bits[1:]: try: lib = get_library(taglib) parser.add_library(lib) except InvalidTemplateLibrary: pass return LoadNode()
def trymenu(parser, token): split = token.contents.split(None) tag_name = split.pop(0) key = split.pop(0) args = split nodelist = parser.parse(('endtrymenu', )) parser.delete_first_token() if 'bambu.navigation' in settings.INSTALLED_APPS: from bambu.navigation.templatetags.navigation import CycleNode return CycleNode(key, nodelist, ' '.join(args)) return LoadNode()
def friendly_load(parser, token): """ Tries to load a custom template tag set. Non existing tag libraries are ignored. This means that, if used in conjunction with ``if_has_tag``, you can try to load the comments template tag library to enable comments even if the comments framework is not installed. For example:: {% load friendly_loader %} {% friendly_load comments webdesign %} {% if_has_tag render_comment_list %} {% render_comment_list for obj %} {% else %} {% if_has_tag lorem %} {% lorem %} {% endif_has_tag %} {% endif_has_tag %} """ bits = token.contents.split() if len(bits) >= 4 and bits[-2] == "from": # from syntax is used; load individual tags from the library name = bits[-1] try: lib = find_library(parser, name) subset = load_from_library(lib, name, bits[1:-2]) parser.add_library(subset) except TemplateSyntaxError: pass else: # one or more libraries are specified; load and add them to the parser for name in bits[1:]: try: lib = find_library(parser, name) parser.add_library(lib) except TemplateSyntaxError: pass return LoadNode()
def import_taglib(parser, token): """ Extends Django's default {% load %} templatetag as a new tag called {% import %}, which allows for extended functionality (while being backwards compatible). Load a tag library from a particular app: {% import myapp:mytaglib %} Load a particular tag from a particular app: {% import mytag from myapp:mytaglib %} Load a particular tag from a particular app and rename: {% import mytag from myapp:mytaglib as othername %} **Note**: you cannot rename multiple tags, so if you do: {% import mytag myothertag from myapp:mytaglib as othername %} then only the last tag will be using othername, and the first one won't be imported. """ bits = token.contents.split() if (len(bits) >= 4 and bits[-2] == "from") or (len(bits) >= 6 and bits[-2] == "as"): lib_index = -1 as_lib = None if (bits[-2] == "as"): lib_index = -3 as_lib = bits[-1] try: taglib = bits[lib_index] lib = get_library(taglib) except InvalidTemplateLibrary as e: raise TemplateSyntaxError("'%s' is not a valid tag library: %s" % (taglib, e)) else: temp_lib = Library() for name in bits[1:(lib_index - 1)]: name_to_use = as_lib if as_lib else name if name in lib.tags: temp_lib.tags[name_to_use] = lib.tags[name] # a name could be a tag *and* a filter, so check for both if name in lib.filters: temp_lib.filters[name_to_use] = lib.filters[name] elif name in lib.filters: temp_lib.filters[name_to_use] = lib.filters[name] else: raise TemplateSyntaxError( "'%s' is not a valid tag or filter in tag library '%s'" % (name, taglib)) parser.add_library(temp_lib) else: for taglib in bits[1:]: # add the library to the parser try: lib = get_library(taglib) parser.add_library(lib) except InvalidTemplateLibrary as e: raise TemplateSyntaxError( "'%s' is not a valid tag library: %s" % (taglib, e)) return LoadNode()