Global variables in Eleventy
In a previous post I discussed how I maintain this website. A big part of it is the use of a static site generator named Eleventy.
In many of my posts I link to my dotfiles repository because that's where the latest versions of configurations and scripts I talk about are stored. This is all fine, until I decided to move to a new git hosting solution. Now I have to go through all my posts to check if I linked to the dotfiles repository in it and change the URL.
This is a pain and something I do not want to have to ever do again. Enter Eleventy's global variables.
Setting up global variables
- Create a file in _data/ named global.js
- Add the following code:
module.exports = {
[NAME_OF_VARIABLE]: "[CONTENT_OF_VARIABLE]",
};
Where [NAME_OF_VARIABLE] is the name you want for this variable, "dotfiles" for example and where [CONTENT_OF_VARIABLE] is the content stored in this variable, which would be the URL to the dotfiles repository in this example, like so:
module.exports = {
dotfiles: "https://link.to.dotfiles/repository",
};
Using the global variables
To use the global variable in a post, call it like this, but wrap the command in double curly braces, two of these { in front and two of these } after:
global.[NAME_OF_VARIABLE]
Where [NAME_OF_VARIABLE] would be "dotfiles" in this example.
Due to the limitations of Eleventy I cannot actually type the curly brackets in the code box above, as the engine would interpret this as an actual global variable, but it should look like this:
- Home
- Next: Moving to Codeberg Pages
- Previous: A simple lockscreen script