2015-07-25 9 views
6

जिंजा में मैं मैक्रो बना सकते हैं और इस तरह से मेरे टेम्पलेट में यह कॉल कर सकते हैं:मैक्रो

{% macro create_list(some_list) %} 
<ul> 
    {% for item in some_list %} 
    <li>{{ item }}</li> 
    {% endfor %} 
</ul> 
{% endmacro %} 

HTML code.... 

{{ create_list(list1) }} 
{{ create_list(list2) }} 
{{ create_list(list3) }} 

मैं Django डॉक्स कि Django टेम्पलेट्स में नहीं पढ़ मैक्रो टैग है। मुझे django टेम्पलेट्स में ऐसा कुछ करने का सबसे अच्छा तरीका है।

उत्तर

8

जैसा कि आपने पहले ही कहा है मैक्रोज़ django की templating भाषाओं में मौजूद नहीं है।

टेम्पलेट्स में अधिक कठिन चीजें करने के लिए टेम्पलेट टैग हैं, लेकिन यह वही नहीं है जो आप खोज रहे हैं, क्योंकि django की templating प्रणाली भी पैरामीटर को पारित करने की अनुमति नहीं देती है।

अपने उदाहरण के लिए सबसे अच्छी बात का उपयोग करने के टैग शामिल होगा:
https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#include

यहां बताया गया है कि मैं इसे का प्रयोग करेंगे:

टेम्पलेट्स/के टुकड़े/list.html

<ul> 
{% for item in list %} 
    <li>{{ item }}</li> 
{% endfor %} 
</ul> 

टेम्पलेट्स/index.html

{% include 'snippets/list.html' with list=list1 %} 
{% include 'snippets/list.html' with list=list2 %} 
{% include 'snippets/list.html' with list=list3 %} 
... 
1

टेम्पलेट/partials/उदाहरण-partial.html

{%if partial_name == 'partial1'%} 
<ul> 
{% for item in list %} 
    <li>{{ item }}</li> 
{% endfor %} 
</ul> 
{%endif%} 


{%if partial_name == 'partial2'%} 
<ul> 
{% for item in list %} 
    <li>{{ item }}</li> 
{% endfor %} 
</ul> 
{%endif%} 


{%if partial_name == 'partial3'%} 
<ul> 
{% for item in list %} 
    <li>{{ item }}</li> 
{% endfor %} 
</ul> 
{%endif%} 

टेम्पलेट्स/index.html

{% include 'partials/example-partial.html' with list=list1 partial_name="partial1"%} 
{% include 'partials/example-partial.html' with list=list2 partial_name="partial2"%} 
{% include 'partials/example-partial.html' with list=list3 partial_name="partial3"%} 
1

मैं दो संकुल की पेशकश करने के लिए मिला:

वे दोनों एक ही काम करने के लिए लग रहे हैं: टेम्पलेट में INSTALLED_APPS, {% load macros %} में डालें, लिखें और उनका उपयोग करें।

0

... बस Django के साथ जिन्जा का उपयोग करना शुरू करें। इसे पर चालू करना बहुत आसान है और आप अलग-अलग फ़ाइलों के लिए एक ही समय में दोनों टेम्पलेट इंजन का उपयोग कर सकते हैं।