Table of Contents
Series:
Project Vision
Eleventy Setup
Directory Structure
Data Handling
Data Output and Templating
Cats
Dogs
Pet Profile Pages and Pagination
Layout and Computed Data
Deployment to Netlify
Accepting Submissions
Conclusion
Home Web Front-end CSS Tutorial A Community-Driven Site with Eleventy: Building the Site

A Community-Driven Site with Eleventy: Building the Site

Apr 02, 2025 pm 04:03 PM

A Community-Driven Site with Eleventy: Building the Site

In the previous article, we explored the planning phase for a community-driven website. We discussed the numerous considerations involved in accepting user submissions, drawing from my experience building Style Stage.

Now, let's dive into the code! We'll build an Eleventy setup that serves as a foundation for your community (or personal) website.

Series:

  1. Planning for Contributions
  2. Building the Site (Current Article)

This article covers:

  • Setting up Eleventy and creating development and build scripts.
  • Recommended configuration customizations.
  • Defining custom data and merging multiple data sources.
  • Creating layouts with Nunjucks and Eleventy layout chaining.
  • Deploying to Netlify.

Project Vision

Imagine a platform where users submit photos of their cats and dogs, competing in cuteness contests.

This article focuses solely on pet submissions; user voting (easily implemented with serverless functions) is left for future development. Users submit their pets' profiles, generating a weekly "battle" featuring a random cat versus a random dog on the homepage.

Eleventy Setup

Begin by initializing a new project using npm init, then install Eleventy:

npm install @11ty/eleventy
Copy after login

Optionally, modify the package.json scripts section:

"scripts": {
  "develop": "eleventy --serve",
  "build": "eleventy"
},
Copy after login

This enables development with Browsersync hot-reloading (npm run develop) and production builds (npm run build).

Install fast-glob for efficient data handling:

npm install --save-dev fast-glob
Copy after login

Directory Structure

Eleventy allows customization of input and output directories. Create eleventy.js at the project root:

module.exports = function (eleventyConfig) {
  return {
    dir: {
      input: "src",
      output: "public"
    },
  };
};
Copy after login

Create a pets directory to store pet data, further subdivided into cats and dogs subdirectories:

<code>pets/
  cats/
  dogs/</code>
Copy after login

Each pet's data will be a JSON file with the following schema:

{
  "name": "",
  "petColor": "",
  "favoriteFood": "",
  "favoriteToy": "",
  "photoURL": "",
  "ownerName": "",
  "ownerTwitter": ""
}
Copy after login

Create a CONTRIBUTING.md file (or README.md) to guide users on submission guidelines and the data schema. Note that favoriteFood, favoriteToy, and ownerTwitter are optional. photoURL should be a URL, not a file upload, for security and hosting reasons.

Data Handling

Create cats.js and dogs.js in the _data directory to combine individual pet files into arrays:

// cats.js
const fastglob = require("fast-glob");
const fs = require("fs");

module.exports = async () => {
  const catFiles = await fastglob("./src/pets/cats/*.json", {
    caseSensitiveMatch: false,
  });

  let cats = new Set();
  for (let cat of catFiles) {
    const catData = JSON.parse(fs.readFileSync(cat));
    cats.add(catData);
  }

  return [...cats];
};
Copy after login

(dogs.js is similar, replacing "cat" with "dog"). This approach minimizes merge conflicts.

Data Output and Templating

Add some sample JSON files to pets/cats and pets/dogs. Create index.njk in the src directory:

<h1 id="Cats">Cats</h1>
Copy after login

Dogs

Pet Profile Pages and Pagination

Create cats.njk and dogs.njk in the src directory with pagination front matter (example for cats.njk):

---
pagination:
  data: cats
  alias: cat
  size: 1
permalink: "https://www.php.cn/link/543817ed62fa34e371bb229d4f7b603f"
---
Copy after login

This generates individual pages for each pet.

Layout and Computed Data

Create base.njk and pets.njk in src/_includes. base.njk provides HTML boilerplate with {{ content | safe }}. Add layout: base.njk front matter to index.njk. Also add layout: base.njk to pets.njk for layout chaining.

Use eleventyComputed in cats.njk and dogs.njk to share template variables:

---
eleventyComputed:
  title: "{{ cat.name }}"
  petColor: "{{ cat.petColor }}"
  favoriteFood: "{{ cat.favoriteFood }}"
  favoriteToy: "{{ cat.favoriteToy }}"
  photoURL: "{{ cat.photoURL }}"
  ownerName: "{{ cat.ownerName }}"
  ownerTwitter: "{{ cat.ownerTwitter }}"
---
Copy after login

Add content to pets.njk to display pet data. Add layout: pets.njk to cats.njk and dogs.njk.

Deployment to Netlify

Deploy the site to Netlify. Configure Netlify to serve from the public directory and run npm run build on merges. A netlify.toml file can simplify this process. Enable deploy previews for pull requests.

Accepting Submissions

Review contribution guidelines and establish branch protection rules. The submission process involves forking, cloning, creating a JSON file, committing changes, opening a pull request, and merging.

Conclusion

This creates a functional site with automated deployment. Further enhancements include email newsletters, social media previews, commenting systems, and Netlify CMS. The complete example is available on GitHub.

The above is the detailed content of A Community-Driven Site with Eleventy: Building the Site. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles