[英]Alias Django Template Tag
因此,我经常需要在Django模板中使用类似以下内容的东西:
{% include "conname.html" %}
我希望能够只拥有自己的标签,而只需键入类似
{% conname %}
是否有一种简单的方法来设置某种别名,以便模板引擎conname
看到conname
标记时都知道它实际上应该是一个特定的include标记?
非常简单,在模块中只需添加以下代码(通常称为custom_tags.py:
from django import template
register = template.Library()
@register.simple_tag
def conname():
return '''{% include "conname.html" %}'''
#to make it available everywhere (i generally add this to my urls.py
from django.template.loader import add_to_builtins
#this will be aded to all templates
add_to_builtins('custom_tags')
在您的模板中,您只需要使用{% conname %}
或者可以使其更加复杂并以编程方式调用IncludeNode
但这{% conname %}
过分了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.