Global Data from the Configuration API Added in v1.0.0 Jump to heading
Contents
In addition to Global Data Files global data can be added to the Eleventy config object using the addGlobalData
method. This is especially useful for plugins.
The first value of addGlobalData
is the key that will be available to your templates and the second value is the value of the value returned to the template.
Example Jump to heading
module.exports = function (eleventyConfig) {
// Values can be static:
eleventyConfig.addGlobalData("myStatic", "static");
// functions:
eleventyConfig.addGlobalData("myFunction", () => new Date());
// or a promise:
eleventyConfig.addGlobalData(
"myFunctionPromise",
() => {
return new Promise((resolve) => {
setTimeout(resolve, 100, "foo");
})
}
);
// or async:
eleventyConfig.addGlobalData(
"myAsyncFunction",
async () => {
return Promise.resolve("hi");
}
);
};
Sources of Data Jump to heading
When the data is merged in the Eleventy Data Cascade, the order of priority for sources of data is (from highest priority to lowest):
- Computed Data
- Front Matter Data in a Template
Front Matter Data in Layouts(only in 0.x)- Template Data Files
- Directory Data Files (and ascending Parent Directories)
- Front Matter Data in Layouts (moved in 1.0)
- Configuration API Global Data ⬅
- Global Data Files