【问题标题】:How simplify calling include with multiple viariables in Django template?如何简化调用包含在 Django 模板中的多个变量?
【发布时间】:2015-09-04 22:23:43
【问题描述】:

我想简化(提高可读性)Django 模板代码:

{% comment %}
required variables:
group_id = 'exclude-brands-group'
select_name = 'exclude-brands'
entities = 'excludeBrandsSets'
add_keyword_name = {% trans 'Brand' %}
url_edit_keywords = {{ url_project_filter_exclude_brands_edit_keywords }}
{% endcomment %}

{% with group_id='exclude-keywords-group' select_name='exclude-keywords' %}
{% with entities=excludeKeywordsSets %}
{% trans 'Words' as add_keyword_name %}
{% with url_edit_keywords=url_project_filter_exclude_keywords %}
{% include 'web_site/seo/frontend/seo/filtered_keyword_idea/template/keyword_filter_group.html' %}
{% endwith %}
{% endwith %}
{% endwith %}

但我不知道如何减少 with 标签的数量 - 因为 include 只是一个行命令。我不知道如何简化反式。您知道将变量传递给模板的更简单方法吗?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    include tag 允许您将额外的上下文传递给模板。这意味着你不需要用标签分开(尽管缺点是你最终会得到很长的行)。没有任何方法可以包含 trans 标记。

    {% trans 'Words' as add_keyword_name %}
    {% include 'web_site/seo/frontend/seo/filtered_keyword_idea/template/keyword_filter_group.html' with group_id='exclude-keywords-group' select_name='exclude-keywords' entities=excludeKeywordsSets url_edit_keywords=url_project_filter_exclude_keywords %}
    

    【讨论】:

    • 如果我需要使用带有with 的模板标签(并且有几个),我猜我仍然需要with 标签,例如:{% with template_tag arg1 arg2 as final_val1 %} {% with template_tag arg2 arg3 as final_val2 %} {%include ....val1=final_val1 val2=final_val2%}跨度>
    • 编辑:我认为简单的标签不需要用于分配,所以我的意思是上面的评论中的以下内容:{% template_tag arg1 arg2 as final_val1 %} {% template_tag arg2 arg3 as final_val2 %} {%include ....val1=final_val1 val2=final_val2%}
    猜你喜欢
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多