如何结合Django内置的"with"标签使用自定义模板标签?

How to Use Custom Template Tag in Combination With Django's Built-in "with" Tag?

我有这个简单的标签:

myapp/templatetags/my_filters.py

@register.simple_tag
def get_bookmark_object(content_type, object_id):
    return Bookmark.objects.get(content_type=content_type, object_id=object_id)

在我的模板中,我希望能够这样做:

{% load my_filters %}

{% with object as bookmark %}
  {% with bookmark_object=get_bookmark_object bookmark.content_type bookmark.object_id %}
    {% if bookmark.content_type.model == 'post' %}
      {% include 'content/post/object.html' with object=bookmark_object user_bookmark=bookmark %}
    {% elif bookmark.content_type.model == 'note' %}
      {% include 'content/note/object.html' with object=bookmark_object user_bookmark=bookmark %}
    {% endif %}
  {% endwith %}
{% endwith %}

我收到错误:

TemplateSyntaxError at /my-page/
'with' received an invalid token: 'bookmark.content_type'

我的问题是:

如何在 with 语句中使用自定义 get_bookmark_object 模板标签?一个带有代码的例子会帮助我澄清很多。

参考: Django's with built-in

你定义的是一个模板标签,你可以将模板标签产生的值赋给一个{% … as … %}标签:

{% get_bookmark_object bookmark.content_type bookmark.object_id <strong>as bookmark_object</strong> %}
    …