首页 > 解决方案 > Django register.tag 将动态变量作为 kwarg

问题描述

我注意到register.simple_tag可以很容易地实现这一点,但不幸的是我现在无法切换到使用它并想用它register.tag来实现它

有一个标签:

register.tag(name='foo')
def foo(parser, token):
    bits = token.split_contents()
    # rest of code..

模板,我有一个我们称之为bar需要传递给foo. 不幸的是,我似乎无法获得动态值 - 下面的 1. 将只是一个字符串 2. 将是一个字符串

1.
{% foo arg='bar' %}
  {{ foo_* }}
{% endfoo %}
2.
{% foo arg=bar %}
  {{ foo_* }}
{% endfoo %}

看起来我可以

{% with b=bar %}
{% foo arg=b %}
  {{ foo_* }}
{% endfoo %}
{% endwith %}

我希望不必在with这里使用语句,而是让“令牌”与 bar 对象一起出现,而不仅仅是获取它的字面量。

标签: djangodjango-templates

解决方案


推荐阅读