- Stable
3.0.0
Toggle Menu
Eleventy
1.93s
Next.js
70.65s
Preprocessors Added in v3.0.0
Contents
The Preprocessor Configuration API allows you to intercept and modify the content in template files (not Layouts) before they’re processed and rendered by Eleventy.
eleventy.config.js
export default function (eleventyConfig) {
eleventyConfig.addPreprocessor("drafts", "njk,md,liquid", (data, content) => {
if(data.draft) {
// Ignore this file.
return false;
}
// You can also modify the raw input of the template here too, be careful!
return `${content}<!-- Template file: {{ page.inputPath }} -->`;
// If you return nothing or `undefined`, no changes will be made to this template.
});
};
module.exports = function (eleventyConfig) {
eleventyConfig.addPreprocessor("drafts", "njk,md,liquid", (data, content) => {
if(data.draft) {
// Ignore this file.
return false;
}
// You can also modify the raw input of the template here too, be careful!
return `${content}<!-- Template file: {{ page.inputPath }} -->`;
// If you return nothing or `undefined`, no changes will be made to this template.
});
};
- The first argument is an arbitrary
name
(String
) used for error messaging. - The second argument can be:
- a
String
of comma separated file extensions - an
Array<String>
of file extensions
- a
- Returning
false
will ignores the template in the same way as usingeleventyConfig.ignores
or.eleventyignore
- Returning nothing or
undefined
has no effect (unlike Transforms)
Originally GitHub Issue #188.
Example: Drafts
Here’s an example that uses the Preprocessor API to implement a Drafts workflow.
Set draft: true
anywhere in a file’s data cascade and that file will be only be built when using Eleventy in --serve
or --watch
modes. It will be excluded from full Eleventy builds.
You might imagine how this could be extended to add a publishing date feature too: to exclude content from builds before a specific date set in a post’s front matter (or elsewhere in the data cascade).
eleventy.config.js
export default function (eleventyConfig) {
eleventyConfig.addPreprocessor("drafts", "*", (data, content) => {
if(data.draft && process.env.ELEVENTY_RUN_MODE === "build") {
return false;
}
});
};
module.exports = function (eleventyConfig) {
eleventyConfig.addPreprocessor("drafts", "*", (data, content) => {
if(data.draft && process.env.ELEVENTY_RUN_MODE === "build") {
return false;
}
});
};