2
我現在有...Django的模板塊
<title>
MyApp | {% block customtitle %}{% endblock %}
</title>
我想真的是爲|
到只有出現,如果customtitle
塊不是空的。如果頁面沒有設置customtitle塊,我只想在標題中看到MyApp
。
這是一個選項嗎?如何實現它?
我現在有...Django的模板塊
<title>
MyApp | {% block customtitle %}{% endblock %}
</title>
我想真的是爲|
到只有出現,如果customtitle
塊不是空的。如果頁面沒有設置customtitle塊,我只想在標題中看到MyApp
。
這是一個選項嗎?如何實現它?
更加簡單定義你的基本模板是這樣的:
<title>
{% block customtitle %}MyApp {% endblock %}
</title>
在派生模板
{% block customtitle %}
{{ block.super }} | Custom Title
{% endblock %}
block.super的存在
然後將確保MyApp的會出現在所有頁面。使用block.super代替硬編碼MyApp將確保將來更改基本模板不會破壞標題。
關於硬編碼MyApp的問題,我只是打算在基本模板中設置它。這將是超級優雅,不必把'| '也在每個派生模板中。而且它會允許該格式在基本模板中也可以更改,而不必編輯所有派生模板。 – Vishal
由於基本模板沒有關於派生模板的信息,因此無法完成。 – e4c5