def add_library(self, library): wrapped_library = Library() wrapped_library.filters = library.filters for name, tag_compiler in library.tags.items(): wrapped_library.tags[name] = self.wrap_compile_function( name, tag_compiler) self.parser.add_library(wrapped_library)
def load(parser, token): """ Loads a custom template tag set. For example, to load the template tags in ``django/templatetags/news/photos.py``:: {% load news.photos %} Can also be used to load an individual tag/filter from a library:: {% load byline from news %} """ # token.split_contents() isn't useful here because this tag doesn't accept variable as arguments bits = token.contents.split() if len(bits) >= 4 and bits[-2] == "from": try: taglib = bits[-1] 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:-2]: if name in lib.tags: temp_lib.tags[name] = 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] = lib.filters[name] elif name in lib.filters: temp_lib.filters[name] = 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()
def load(parser, token): """ Loads a custom template tag set. For example, to load the template tags in ``django/templatetags/news/photos.py``:: {% load news.photos %} Can also be used to load an individual tag/filter from a library:: {% load byline from news %} """ bits = token.contents.split() if len(bits) >= 4 and bits[-2] == "from": try: taglib = bits[-1] lib = get_library(taglib) except InvalidTemplateLibrary, e: raise TemplateSyntaxError("'%s' is not a valid tag library: %s" % (taglib, e)) else: temp_lib = Library() for name in bits[1:-2]: if name in lib.tags: temp_lib.tags[name] = 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] = lib.filters[name] elif name in lib.filters: temp_lib.filters[name] = 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)
from __future__ import unicode_literals from django.template.base import Library from django.template.defaultfilters import stringfilter register = Library() @register.filter(is_safe=True) @stringfilter def dropcap(value): """ Wraps the first character in a <span> tag with a .dropcap class that can be used to make it bigger than the surrounding text. """ return value and "<span class='dropcap'>%s</span>%s" % (value[0].upper(), value[1:]) @register.filter(is_safe=True) @stringfilter def emdashes(html): """ Replace any '--' with '—' """ return html.replace("--", "—")
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()