本文整理汇总了Python中django.template.base.Library.tags[name_to_use]方法的典型用法代码示例。如果您正苦于以下问题:Python Library.tags[name_to_use]方法的具体用法?Python Library.tags[name_to_use]怎么用?Python Library.tags[name_to_use]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.base.Library
的用法示例。
在下文中一共展示了Library.tags[name_to_use]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_taglib
# 需要导入模块: from django.template.base import Library [as 别名]
# 或者: from django.template.base.Library import tags[name_to_use] [as 别名]
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()