You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遍历cupcakesTest文件夹遇问题,需用for循环实现内容遍历

Hey there! Let's work through why your for loop isn't pulling in content from the cupcakesTest folder properly. Here's the breakdown of the issues and how to fix them:

1. Register cupcakesTest as a Jekyll Collection

Jekyll doesn't automatically recognize custom folders as content collections by default. You need to tell Jekyll to process this folder by adding a configuration to your _config.yml file:

collections:
  cupcakesTest:
    output: false # Set this to `true` if you want individual pages generated for each cupcake file

This tells Jekyll to scan the cupcakesTest folder, parse the Markdown files inside, and make them available via site.cupcakesTest in your templates.

2. Clean Up Your Template Code

Your current code has a redundant line ({{ site.cupcakesTest }}) that just outputs the raw collection object (not useful for display). Plus, without the collection registered, that variable would be empty anyway. Here's the corrected template:

---
layout: post
title: Muffins
---
<h1>Our Cupcakessssss</h1>
<div class="cupcakes">
  {% for cupcake in site.cupcakesTest %}
    <div class="cupcake">
      <div class="image"><img src="{{ cupcake.image_path }}"/></div>
      <h2>{{ cupcake.type }}</h2>
    </div>
  {% endfor %}
</div>

3. Verify Your Cupcake Markdown Files

Each file in cupcakesTest needs valid YAML front matter with the fields you're trying to access (type and image_path). For example, a file named cupcakesTest/chocolate.md should look like this:

---
type: Chocolate Fudge Cupcake
image_path: /assets/images/chocolate-cupcake.jpg # Adjust path to match your site's image location
---
Optional: Add any extra details about the chocolate cupcake here.

Make sure the image_path points to the correct location of your image file (common locations are assets/images/ or images/ in your Jekyll project).

After making these changes, restart your Jekyll server (jekyll serve) to apply the config update, and your loop should correctly render each cupcake's image and type!

内容的提问来源于stack exchange,提问作者Manu Mohanan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:04:49