2013-06-25 20 views
0

这个例子在http://djangosnippets.org/snippets/2421/原样。使用Django沿着模板传递它的活动菜单

模板放在/包括/ tabs.html

<ul class="tab-menu"> 
     <li class="{% if active_tab == 'tab1' %} active{% endif %}"><a href="#">Tab 1</a></li> 
     <li class="{% if active_tab == 'tab2' %} active{% endif %}"><a href="#">Tab 2</a></li> 
     <li class="{% if active_tab == 'tab3' %} active{% endif %}"><a href="#">Tab 3</a></li> 
    </ul> 

在page.html中的模板放在

{% include "includes/tabs.html" with active_tab='tab1' %} 

的变量被沿着从页面模板传递给tabs.html 。

你将如何从通有源标签变量:

- page.html (pass active tab1) 
    - extends base.html 
     - includes tabs.html (how to get it here.) 

回答

0

在你base.html你可以有你的{% block tabs %}

{% block tabs %}{% endblock %} 

而在你page.html

{% extends "base.html" %} 
{% block tabs %} 
    {% include "includes/tabs.html" with active_tab='tab1' %} 
{% endblock %} 

如果这是不够的干你,你可以创造一个自定义inclusion tag为您的标签。