如何重复部分 Jinja2 模板
How to Repeat Parts of Jinja2 Template
我意识到有人问了几个类似的问题,但它们与我正在尝试做的事情并没有特别相关,而且我几乎是一个初学者,所以答案似乎比我认为我应该做什么。
我正在努力将带有 Python 代码的 Jinja2 模板实现到 html 页的笔记中,我在学习这门课程时一直在做笔记。
所以...
我尝试模板化并重复的 html 块具有以下结构:
<h1>Stage Number</h1>
<div class = "lesson">
<h2>Lesson Number</h2>
<div class="concept">
<div class="concept-title"> Title </div>
<div class="concept-description">
<p>Description paragraph</p>
<p>Description paragraph</p>
</div>
<div class="concept-title"> Title</div>
<div class="concept-description">
<p>Description paragraph</p>
</div>
</div>
</div>
对于每个阶段,课程编号各不相同,每个标题都有不同数量的描述段落。
我的代码在 Github(这是编辑过的版本):https://github.com/graceehayden/Stage4Udacity-Session-2/blob/master/templates/index.html
我的main.py文件中有模板代码,应该在index.html文件中实现。
这门课程对我来说有点过头了,re-watching 视频或寻找其他关于模板的 YouTube 教程都没有帮助,因为其中很多比我现在看起来更高级.
任何关于如何理顺变量以便它们与 index.html 文件正确关联的帮助或指示将不胜感激。当我假装我没有那么多输入时,我能够使一个简单的单一变量工作并在我 运行 应用程序时显示出来,但是由于我现在拥有和需要的复杂性,它无法正常工作.
到目前为止,看起来您对代码的想法大多是正确的。
缺少的一件大事是您需要序列数据类型来扩展它以执行您在这里想要的操作。
每节课都有一个概念列表和相关说明。
concept1 = {
'name' : 'concept1',
'description1' : 'This is the description for concept1!',
}
lesson1 = {
'name' : 'lesson1',
'concepts': [concept1, concept2] #the list here lets us have more than one value
}
那么每个 Stage 都是一个包含不同课程列表的数据结构。
stage1 = {
'name' : 'stage1',
'lessons': [lesson1, lesson2],
}
最后你把所有这些不同的阶段都塞进了 template_values
template_values = {
'stages' : [stage1, stage2],
}
然后您需要模板来访问嵌套数据:
{% for stage in stages %}
//do things with this particular stage
{% for lesson in stage['lessons'] %}
//do things with this lesson
{% for concept in lesson['concepts'] %}
//do things with individual concepts
{% endfor %}
{% endfor %}
{% endfor %}
注意:尚未测试模板,因此可能在访问字典时出错。
我意识到有人问了几个类似的问题,但它们与我正在尝试做的事情并没有特别相关,而且我几乎是一个初学者,所以答案似乎比我认为我应该做什么。
我正在努力将带有 Python 代码的 Jinja2 模板实现到 html 页的笔记中,我在学习这门课程时一直在做笔记。
所以...
我尝试模板化并重复的 html 块具有以下结构:
<h1>Stage Number</h1>
<div class = "lesson">
<h2>Lesson Number</h2>
<div class="concept">
<div class="concept-title"> Title </div>
<div class="concept-description">
<p>Description paragraph</p>
<p>Description paragraph</p>
</div>
<div class="concept-title"> Title</div>
<div class="concept-description">
<p>Description paragraph</p>
</div>
</div>
</div>
对于每个阶段,课程编号各不相同,每个标题都有不同数量的描述段落。
我的代码在 Github(这是编辑过的版本):https://github.com/graceehayden/Stage4Udacity-Session-2/blob/master/templates/index.html
我的main.py文件中有模板代码,应该在index.html文件中实现。
这门课程对我来说有点过头了,re-watching 视频或寻找其他关于模板的 YouTube 教程都没有帮助,因为其中很多比我现在看起来更高级.
任何关于如何理顺变量以便它们与 index.html 文件正确关联的帮助或指示将不胜感激。当我假装我没有那么多输入时,我能够使一个简单的单一变量工作并在我 运行 应用程序时显示出来,但是由于我现在拥有和需要的复杂性,它无法正常工作.
到目前为止,看起来您对代码的想法大多是正确的。 缺少的一件大事是您需要序列数据类型来扩展它以执行您在这里想要的操作。
每节课都有一个概念列表和相关说明。
concept1 = {
'name' : 'concept1',
'description1' : 'This is the description for concept1!',
}
lesson1 = {
'name' : 'lesson1',
'concepts': [concept1, concept2] #the list here lets us have more than one value
}
那么每个 Stage 都是一个包含不同课程列表的数据结构。
stage1 = {
'name' : 'stage1',
'lessons': [lesson1, lesson2],
}
最后你把所有这些不同的阶段都塞进了 template_values
template_values = {
'stages' : [stage1, stage2],
}
然后您需要模板来访问嵌套数据:
{% for stage in stages %}
//do things with this particular stage
{% for lesson in stage['lessons'] %}
//do things with this lesson
{% for concept in lesson['concepts'] %}
//do things with individual concepts
{% endfor %}
{% endfor %}
{% endfor %}
注意:尚未测试模板,因此可能在访问字典时出错。