Nunjucks for 循环运行两次
Posted
技术标签:
【中文标题】Nunjucks for 循环运行两次【英文标题】:Nunjucks for loop running twice 【发布时间】:2021-11-21 09:01:37 【问题描述】:我正在使用 Nunjucks 和 Eleventy 构建一个网站。我正在尝试使用 Nunjucks for 循环创建一个无序列表(数据存储在 JSON 文件中)。一切似乎都在工作,除了循环运行了两次,而它应该只运行一次。
JSON
"catalog":[
"title":"Title #1","author":"Author #1","edition":"Third","isbn":"19384579823745","available":"0","total":"4",
"title":"Title #2","author":"Author #2","edition":"Fifth","isbn":"1390458093484","available":"2","total":"2",
"title":"Title #3","author":"Author #3","edition":"First","isbn":"8974692873489","available":"1","total":"3",
"title":"Title #4","author":"Author #4","edition":"Third","isbn":"19384579823745","available":"0","total":"4",
"title":"Title #5","author":"Author #5","edition":"Fifth","isbn":"19384579823745","available":"0","total":"4",
"title":"Title #6","author":"Author #6","edition":"First","isbn":"1390458093484","available":"2","total":"2",
"title":"Title #7","author":"Author #7","edition":"First","isbn":"19384579823745","available":"0","total":"4",
"title":"Title #8","author":"Author #8","edition":"First","isbn":"1390458093484","available":"2","total":"2",
"title":"Title #9","author":"Author #9","edition":"Second","isbn":"19384579823745","available":"0","total":"4",
"title":"Title #10","author":"Author #10","edition":"Second","isbn":"1390458093484","available":"2","total":"2",
"title":"Title #11","author":"Author #11","edition":"Second","isbn":"8974692873489","available":"1","total":"3"
]
修女
% if sheet.catalog %
<ul class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4">
% for textbook in sheet.catalog %
<li class="m-10"><strong> textbook.title </strong><br /> textbook.author <br />Edition: textbook.edition <br />ISBN: textbook.isbn <br />Available: textbook.available of textbook.total </li>
% endfor %
</ul>
% else %
<p>We're sorry. The textbook catalog has encountered an error.</p>
% endif %
HTML 输出
<ul class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4">
<li class="m-10"><strong>Title #1</strong><br>Author #1<br>Edition: Third<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #2</strong><br>Author #2<br>Edition: Fifth<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #3</strong><br>Author #3<br>Edition: First<br>ISBN: 8974692873489<br>Available: 1 of 3</li>
<li class="m-10"><strong>Title #4</strong><br>Author #4<br>Edition: Third<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #5</strong><br>Author #5<br>Edition: Fifth<br>ISBN: 19384579823745<br>Available: 0 of 4</li><li class="m-10"><strong>Title #6</strong><br>Author #6<br>Edition: First<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #7</strong><br>Author #7<br>Edition: First<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #8</strong><br>Author #8<br>Edition: First<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #9</strong><br>Author #9<br>Edition: Second<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #10</strong><br>Author #10<br>Edition: Second<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #11</strong><br>Author #11<br>Edition: Second<br>ISBN: 8974692873489<br>Available: 1 of 3</li>
<li class="m-10"><strong>Title #1</strong><br>Author #1<br>Edition: Third<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #2</strong><br>Author #2<br>Edition: Fifth<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #3</strong><br>Author #3<br>Edition: First<br>ISBN: 8974692873489<br>Available: 1 of 3</li>
<li class="m-10"><strong>Title #4</strong><br>Author #4<br>Edition: Third<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #5</strong><br>Author #5<br>Edition: Fifth<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #6</strong><br>Author #6<br>Edition: First<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #7</strong><br>Author #7<br>Edition: First<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #8</strong><br>Author #8<br>Edition: First<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #9</strong><br>Author #9<br>Edition: Second<br>ISBN: 19384579823745<br>Available: 0 of 4</li>
<li class="m-10"><strong>Title #10</strong><br>Author #10<br>Edition: Second<br>ISBN: 1390458093484<br>Available: 2 of 2</li>
<li class="m-10"><strong>Title #11</strong><br>Author #11<br>Edition: Second<br>ISBN: 8974692873489<br>Available: 1 of 3</li>
</ul>
知道为什么 for 循环会运行两次吗?非常感谢您的帮助!
【问题讨论】:
如果你删除你的输出,比如完全删除它,然后重新创建,你还能看到它吗? 我的输入文件夹中保存了一份 JSON 文件的副本。我删除了它,现在 html 输出不再显示渲染的数据。谢谢你,@RaymondCamden! 如果您不介意,请将此分辨率添加为答案,然后将其标记为正确的。 【参考方案1】:在我的输入文件夹中保存了一份 JSON 文件的副本。我删除了它,现在 HTML 输出不再显示渲染的数据。谢谢@RaymondCamden!
【讨论】:
以上是关于Nunjucks for 循环运行两次的主要内容,如果未能解决你的问题,请参考以下文章
Javascript:for..in 循环运行的次数超出预期