2013-05-07 116 views
0

比方说,我有这样的结构:如何覆盖Django中包含的模板块?

{# base.html #} 
{% block content %}{% endblock %} 

{# page.html #} 
{% extends "base.html" %} 
{% block content %} 
{% include "snippet.html" %} 
{# and I also want somehow to redefine {% block snippet_content %} of snippet here #} 
{% endblock %} 

{# snippet.html #} 
<bells_and_whistles> 
    {% block snippet_content %}{% endblock %} 
</bells_and_whistles> 

我希望代码是自我解释。

有没有一种优雅的方式来实现这一目标?

回答

0

恐怕这是不可能的,你想这样做。

的选项有:

  1. 创建modified_snippet.htmlsnippet.html继承和重写块,包括它,而不是
  2. snippet_content块更改为{{ snippet_content }}变量和使用{% include "snippet.html" with snippet_content="My content" %}传递其内容。当然这种方法非常有限。