最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nunjucks - Having trouble creating individual events pages from _dataevents.json - Stack Overflow

programmeradmin4浏览0评论

I have an events.json file in my _data folder. It contains a list of events, grouped by date.

{
    "2025-03-10": [
        {
            "slug": "confidence-building-training-03-10",
            "time": "15:00",
            "event": "Confidence Building Training",
            "description": "Information about the training session.",
            "location": "1 Any Street, This Place, A12 3BC",
            "aniser": "Nathan Hawkes",
            "image": "/images/events/confidence-workshop.jpg"
        }
    ],
    "2025-04-10": [
        {
            "slug": "confidence-building-training-04-10",
            "time": "15:00",
            "event": "Confidence Building Training",
            "description": "Information about the training session.",
            "location": "1 Any Street, This Place, A12 3BC",
            "aniser": "Nathan Hawkes",
            "image": "/images/events/confidence-workshop.jpg"
        },
        {
            "slug": "confidence-building-training-04-10-02",
            "time": "18:00",
            "event": "Evening Confidence Workshop",
            "description": "An additional session for those who prefer evening workshops.",
            "location": "2 Another Road, That Town, B34 5CD",
            "aniser": "Nathan Hawkes",
            "image": "/images/events/evening-workshop.jpg"
        }
    ],
    "2025-05-20": [
        {
            "slug": "confidence-building-training-05-20",
            "time": "15:00",
            "event": "Confidence Building Training",
            "description": "Information about the training session.",
            "location": "1 Any Street, This Place, A12 3BC",
            "aniser": "Nathan Hawkes",
            "image": "/images/events/confidence-workshop.jpg"
        }
    ]
}

I have an event.11ty.js file in my src folder:

module.exports = function(eleventyConfig) {
    const eventsData = require("./_data/events.json"); // Load the events data
  
    let eventPages = []; // Array to hold each page's data
  
    // Loop through the events and create a page for each one
    Object.keys(eventsData).forEach((date) => {
      eventsData[date].forEach((event) => {
        // Create an object for each event with permalink and data
        eventPages.push({
          permalink: `/event/${event.slug}/`, // Set the URL for the page
          data: {
            layout: "event.njk", // Specify the layout for the page
            event: event.event, // Event name
            time: event.time, // Event time
            description: event.description, // Event description
            location: event.location, // Event location
            aniser: eventaniser, // Event aniser
            image: event.image // Event image
          }
        });
      });
    });
  
    return eventPages; // Return an array of event page data
  };

I have an events calendar that uses the JSON file to add events to the calendar. It then links to each individual event page. However, when I run npx eleventy --serve, I am getting this in Terminal:

[11ty] Problem writing Eleventy templates:
[11ty] 1. Having trouble writing to "./public/event/index.html" from "./src/event.11ty.js" (via EleventyTemplateError)
[11ty] 2. The return value from the render function for the 11ty.js template was not a String or Buffer. Received [object Object],[object Object],[object Object],[object Object]

I have been trying multiple things, even using ChatGPT to try and find solutions (I know, I know). Nothing seems to be working. Is there any help anyone can give me?

发布评论

评论列表(0)

  1. 暂无评论