【问题标题】:How to use nested template tags with arguments?如何使用带有参数的嵌套模板标签?
【发布时间】:2025-12-07 20:05:01
【问题描述】:

我想了解如何使用嵌套模板标签,其中子模板标签采用如下参数:

{% parent_tag {% child_tag arguments %} rest_of_parent_arguments %}

在上面一行中,我想使用子标签的返回值作为父标签的参数。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    我会用自定义过滤器替换 child_tag。比如:

    {% parent_tag argument1|filtername:argument2 rest_of_parent_arguments %}
    

    假设“参数”最多包含 2 个参数。有关自定义过滤器,请参见此处:

    http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters

    【讨论】:

    • 感谢您的回答。这是我的问题的确切解决方案。