2011-10-18 43 views
0

我正在为Django编写一个templatetag软件包,以便在Django应用程序中轻松包括RGraph图。我可以用Django templatetags覆盖父模板中的块吗?

我对包含javascript的模板有点麻烦,我在我的基本html文件中定义了一个块,我想要templatetag的模板提供。

这是我的最高水平模板

{% load django_rgraph %} 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
    <title>Test Charts</title> 
    {% block js %} 
    {% block js.custom %}{% endblock %} 
    {% endblock %} 
</head> 

<body> 

{% rgraph piechart %} 

</body> 
</html> 

,这是我的新标签模板

{% extends "django_rgraph/rgraph_base.html" %} 

{% block rgraph_chart %} 
<script> 
window.onload = function() { 
    var {{ chart.name }} = new RGraph.Pie('{{ chart.name }}', [{% for value in chart.values %}{{ value }}{% if not forloop.last %},{% endif %}{% endfor %}]); 
    {% for option, value in chart.options.items %} 
    {{ chart.name }}.Set('{{ option }}', {{ value }}); 
    {% endfor %} 

    {% if chart.animate %} 
    RGraph.Effects.Pie.RoundRobin({{ chart.name }}); 
    {% else %} 
    {{ chart.name }}.Draw();  
    {% endif %} 
} 
</script> 
{% endblock %} 

和完整性,rgraph_base.html看起来像这样

{% block js.custom %} 
{% for js in chart.js %} 
<script src="RGraph/js/{{ js }}"></script> 
{% endfor %} 
{% endblock %} 

{% block rgraph_chart %}Insert Chart Here{% endblock %} 

我希望这会创建一个html页面,其中的JavaScript被包含在标题中,而是我牛逼出现在身上,这样

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
    <title>Test Charts</title> 
</head> 

<body> 

<!-- I was expecting these script tags to be below the title tag --> 
<script src="RGraph/js/RGraph.common.core.js"></script> 
<script src="RGraph/js/RGraph.common.tooltips.js"></script> 
<script src="RGraph/js/RGraph.common.effects.js"></script> 
<script src="RGraph/js/RGraph.pie.js"></script> 

<script> 
window.onload = function() { 
    var pie1 = new RGraph.Pie('pie1', [10,24,15,82]); 
    pie1.Set('chart.gutter.left', 30); 
    pie1.Draw(); 
} 
</script> 
</body> 
</html> 

在此设置下,在新标签覆盖在顶层模板中定义的块模板,我应该能够使脚本出现在顶部头标记?

+0

该脚本出现在身体中,因为rgraph标签被插入身体。没有? – akonsu

+0

@akonsu,这就是我要求的,在标签的模板中有一个与父类中的某个相同名称的块,我期待着与普通模板相同的行为,即,子块中的块会覆盖块中的块父母。 –

+1

显然,Django模板并不像这样。此外,您所期望的行为是否正确也值得怀疑。毕竟你只是将你的标签插入身体。您不是从具有要填充的块的顶级模板派生而来的。 – akonsu

回答

1

Akonsu是对的,你有一个django模板标签的误解,他们不能覆盖其他块。

相关问题