Is there any way to use the return element of templatetags by aliasing ?
For example,
templatetags.py开发者_开发知识库
@register.simple_tag()
def getCaptionLabel(form,pid):
return form.__getitem__('caption_%s'%pid)
mypage.html
{% with field = getCaption form photo.pk %}
{{ field.label_tag }}
No, you have to actually do it in your tag. The Django trunk now has an assignment_tag
(See: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#assignment-tags) to allow you to do this easily, but until it hits a release, you'll have to do it the old fashioned way. See: https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#setting-a-variable-in-the-context
精彩评论