2016-09-18 8 views
2

base.htmlファイルにincludeextendsを使用し、それらを順番に含めることを期待しています。ただし、extendsテンプレートがファイルの末尾に追加されます。私は私のテンプレートが私の出力与えることを期待していJinja2 include&extendが正常に動作しない

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<p>Test String from block !</p> 
<footer>text from footer.</footer> 
</body> 
</html> 

が、現在の結果は以下のとおりです。base.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<footer>text from footer.</footer> 
</body> 
</html> 
     <p>Test String from block !</p> 

、最初に私は、その後header.htmlcontent.html、その後footer.htmlたが含まレンダリング注文はheader.html,footer.html,content.htmlです。

index.html

{% extends "base.html" %} 
{% block content %} 
    <p>Test String from block !</p> 
{% endblock %} 

base.html

{% include "header.html" %} 
<body> 
    {% extends "content.html" %} 
{% include "footer.html" %} 

header.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 

content.html

{% block content %} 

{% endblock %} 

footer.html

<footer>text from footer.</footer> 
</body> 
</html> 

答えて

2

include代わりのextendsを使用したいと思います。私はこのような構造を最近使用し、適切な結果を得ました。

のindex.html:

{% extends "base.html" %} 

{% block head %} 
    <!-- if you want to change the <head> somehow, you can do that here --> 
{% endblock head %} 

{% block content %} 
<body> 
    <p>Test String from block !</p> 
    {% include 'content.html' %} 
</body> 
{% include 'footer.html' %} 
{% endblock content %} 

base.html:

<!DOCTYPE html> 
    <html lang="en"> 
    <head> 
     {% block head %} 
     <meta charset="UTF-8"> 
     <title>Title</title> 
     {% endblock head %} 
    </head> 
    {% block content %} 
    {% endblock content %} 
</html> 

content.html:

<!-- whatever you want in here --> 

footer.html:

<footer>text from footer.</footer> 

うまくいけば助けてください。

+0

これはなぜ以前に起こったのですか?フラスコ/ジンジャーのバグか、それとも悪いですか? –

+0

ファイルがどうにかして引っ張られていたのは問題だったと思うが、確かにそのことは言えない。 – coralvanda

+0

あなたの答えは素晴らしいです.iはまた、 'jinja' github repository.thanksに問題を作るでしょう –

0

私はあなたが私が若干異なる構造を示唆している

{% extends "content.html" %} 
+0

私は{%block%}を使用しているので動作しません –

関連する問題