【发布时间】:2011-11-29 14:00:51
【问题描述】:
我已经阅读了custom tags and filters documentation,但我不知道如何做到这一点。我想制作一个只呈现字符串的自定义标签。没有上下文,每次都是相同的字符串文字。在这种特殊情况下,我更喜欢这样而不是把 {% include 'hello_world.htm' %} 到处都是:
Foo, foo foo
<br>
{% hello_world %}
<br>
bar bar bar
呈现给:
Foo, foo foo
<br>
"Hello World"
<br>
bar bar bar
我觉得我应该能够通过以下方式做到这一点:
custom_tags.py:
from django import template
register = template.Library()
@register.inclusion_tag('hello_world.htm')
def hello_world():
return {}
# Or:
def hello_world():
return {}
register.inclusion_tag('hello_world.htm', takes_context=False)(hello_world)
没有骰子。我在 custom_tags.py 中有其他自定义标签,我正在加载它们,它们工作正常,但总是得到 p>
Invalid block tag: 'hello_world', expected 'endblock' or 'endblock content'
文档说
标签比过滤器更复杂,因为标签可以做任何事情。
...你如何用标签做最简单的事情?
【问题讨论】: