これを行うハックな方法を見つけました。私はそれにとても満足していません。シンプルなブロックを使用して、テンプレートのどのセクションをタグif
でレンダリングするかを切り替えることができることがわかりました。include
これにより、参照とコンテンツを別々に含めることができます。(注意してください。参照とコンテンツを別々のファイルに分けることで、この問題を解決できます。しかし、それはこの解決策よりも退屈なようです。)
共有テンプレートを他のテンプレートから分離できるため、このソリューションは現在の回答よりも優れています。このモジュラー デザインを維持することは、組み合わせて使用できる機能を使用する場合に重要です (これは、共有テンプレートで行いたいことです)。
テンプレート:
{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
<link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% include "mySharedTemplate.html" with references="True" %}
{% endblock %}
...
{% include "mySharedTemplate.html" with content="True" %}
...
共有テンプレート:
{% if references %}
{% load staticfiles %}
<link rel="stylesheet" href="{% static "mySharedTemplateStylesheet.css" %}" type="text/css">
{% endif %}
{% if content %}
...
{% endif %}
私のモジュラー設計が重要であると考える理由を説明するために:
多数の共有テンプレートと多数の通常のテンプレートがあり、それぞれが共有テンプレートをさまざまな方法で使用しているとします。私のモジュラー方式により、通常のテンプレートが共有テンプレートと最も適した柔軟な方法で簡単に連携できるようになります。
テンプレート 2:
{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
<link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% include "mySharedTemplate.html" with references="True" %}
{% include "mySharedTemplate2.html" with references="True" %}
{% endblock %}
...
{% include "mySharedTemplate.html" with content="True" %}
{% include "mySharedTemplate2.html" with content="True" %}
...
テンプレート 3:
{% extends "layout.html" %}
{% load staticfiles %}
{% block references %}
<link rel="stylesheet" href="{% static "myStylesheet.css" %}" type="text/css">
{% include "mySharedTemplate2.html" with references="True" %}
{% include "mySharedTemplate3.html" with references="True" %}
{% include "mySharedTemplate4.html" with references="True" %}
{% endblock %}
...
{% include "mySharedTemplate4.html" with content="True" %}
{% include "mySharedTemplate3.html" with content="True" %}
{% include "mySharedTemplate2.html" with content="True" %}
...
テンプレート 2 とテンプレート 3 では、ボイラー プレート コードをあまり使わなくても、共有テンプレートを適切な方法で使用できることに注意してください。