- CSS 54.2%
- HTML 35.8%
- JavaScript 4.2%
- Nunjucks 3.9%
- Python 1.9%
| .vscode | ||
| _site | ||
| netlify/functions | ||
| node_modules | ||
| src | ||
| .eleventy.js | ||
| .gitignore | ||
| .netlify.toml | ||
| generate_rss.py | ||
| LICENSE | ||
| package-lock.json | ||
| package.json | ||
| README.MD | ||
Instructions
Part 1: Install Eleventy
- Open terminal in VSCode.
- Open terminal
- Type
npm init - Type
npm install @11ty/eleventy - Go to
package.jsonand locate thescriptselement - Replace
"test": "echo \"Error: no test specified\" && exit 1"with"start": "eleventy --serve", "build": "eleventy"- Rewrites the command that enables launching a local server with hot reload (automatic updates to live server while making changes to the code)
- In the root folder, create
.eleventy.js- helps to customize many elements of the repo such as input and output folders.
- In
.eleventy.jstypemodule.exports = function (eleventyConfig){ return { dir: { input: "src", output: "public", }, }; };which makes thesrcfolder the one where the codes is edited andpublicfolder the one that is deployed.
Part 2: Create a Site
- In root directory, create a folder called
src. This will be a directory. - Inside
src, create a file calledindex.html. - Add some boilerplate HTML content.
- Back in the command line, run the start command
npm start. Click the local host:8000 link that appears. - Return to the code editor. Inside the
srcfolder, create the filecss/style.css. This will create a folder with a stylesheet. - Inside
style.cssadd some boilerplate CSS of choice. - Go back to
index.htmland a link to the stylesheet in theheadelement like so:<link rel="stylesheet" href="css/style.css">. While this would have applied the stylesheet, at the time of the video tutorial and writing this, Eleventy requires one more step. - Go to
.eleventy.js(the config file) and add right belowmodule.exports = function (eleventyConfig){add the following two lineseleventyConfig.addPassthroughCopy("./src/css/"); eleventyConfig.addWatchTarget("./src/css/");. If the file doesn't update, killing the terminal and starting up another server could fix it.
Part 3: Templates
- In the
srcdirectory, create another directory called_includes/base.njkwhich will hold all website files. 16.5 If Nunjucks Emmet isn't working on your editor, download a Nunjucks extension and on the settings.json addcode. You can also try changing the language the editor autodetects toHTMLinstead. Check this tip. - On
base.njk, add some boiler plate HTML. Link the stylesheet on the head element like before. Inside the body tags, the following:
<header> <h1>{{ title }}</h1> </header> <main> <article> {{ content | safe }} </article> </main> <footer> <p>© 2021 Arima Mary</p> </footer>. Additionally, change the text inside the title tag inside the head tag for {{ title }}.
- Go to
index.htmland change it's extension to Mardown such that the file becomesindex.md. Then, add the following at the top of the file,
---
title: Base Eleventy Project
layout: base.njk
---
and replace the rest of the content inside the content inside the body tag with Markdown syntax. This is YAML frontmatter. The first line adds a title to the page and the second applies the layout described in base.njk plus stylesheet.
-
In the
srcdirectory, create the folderpagesand the filepage.njkseparately. -
Inside
page.njk, paste the following:---layout: base.njk---
{{ content | safe }}
-
Inside
pagesdirectory, create the filespage-one.mdandpage-two.md. -
In both pages, add the following content:
---
title: Page (One/Two)
---
This is the (first/second) page
- To add a layout to all files without repeating the frontmatter in the
pagesdirectory, create a file calledpages.jsonand add the following:
{
"layout": "page.njk",
"tags": "pages"
}
The tags property allows to use the directory as a collection.
- Back in
index.md, add the following to display the pages collection as a list.
{% for page in collections.pages %}
- [{{ page.data.title }}]({{ page.url }})
{%- endfor %}
This uses a combination of Markdown syntax link for link: {{ page.data.title }} and URL: {{ page.url }}
If the live page doesn't update. Try killing it and restarting using npm start in the terminal.
Extra: How to add an image.
-
Inside the
srcdirectory, create a folder calledimagesand add a stock image. -
Inside
inside.mdadd the Markdown syntax to display and image like so,. -
In
.eleventy.jsafter theaddWatchTargetforsrc/cssfolder add,
eleventyConfig.addPassthroughCopy("./src/images/");
This should display your image of choice. If it doesn't, check the public folder to see if the image folder was created inside the src directory. If not, kill the serve and restart 11ty with npm start. Hope it works!