2015-09-04 62 views
0

我想簡化(使其可讀性更強)的Django模板代碼:在Django模板中如何簡化調用包含多個viariables?

{% 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 %} 

但是我沒有想法如何減少數量與標籤 - 因爲包括只有一行命令。我不知道如何簡化trans。你知道一些簡單的方法將變量傳遞給模板嗎?

回答

2

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 %} 
+0

如果我需要使用帶'with'一個模板標籤(並有那幾個),猜我仍然需要在'with'標記,如:'{%與template_tag ARG1 ARG2爲final_val1 %} {%with template_tag arg2 arg3 as final_val2%} {%include .... val1 = final_val1 val2 = final_val2'%} – Anupam

+0

編輯:我覺得簡單的標籤不需要用於賦值,所以我的意思是上面的以下內容評論:'{%template_tag arg1 arg2 as final_val1%} {%template_tag arg2 arg3 as final_val2%} {%include .... val1 = final_val1 val2 = final_val2%}' – Anupam

相關問題