Menu
- Why Eleventy?
- Get Started
- Community
- Working with Templates
- Using Data
- Configuration
- Template Languages
- Plugins
- API Services
- Release History
- Advanced
5.81s
40.14s
Filters
Contents
A filter is a function which can be used within templating syntax to transform data into a more presentable format. Filters are typically designed to be chained, so that the value returned from one filter is piped into the next filter.
Various template engines can be extended with custom filters to modify content. Here are a few examples:
<h1>{{ name | makeUppercase }}</h1>
<h1>{{ name | makeUppercase }}</h1>
module.exports = function({name}) {
return `<h1>${this.makeUppercase(name)}</h1>`;
};
<h1>{{ makeUppercase name }}</h1>
Filters can be added using the Configuration API and are available to multiple template engines, simultaneously. They are currently supported in JavaScript , Markdown, Nunjucks, Liquid, Handlebars, and WebC.
module.exports = function(eleventyConfig) {
eleventyConfig.addFilter("makeUppercase", function(value) { … });
// New in v2.0.0
eleventyConfig.addAsyncFilter("makeUppercase", async function(value) { … });
};
Read more about filters on the individual Template Language documentation pages:
Eleventy Provided Filters Jump to heading
We also provide a few universal filters, built-in:
url
: Normalize absolute paths in your content, allows easily changing deploy subdirectories for your project.slugify
:"My string"
to"my-string"
for permalinks.log
:console.log
inside templates.get*CollectionItem
: Get next or previous collection items for easy linking.
Access existing filters in your Configuration File Jump to heading
If you’d like to reuse existing filters, you can use the Configuration API’s getFilter
method. When called with a valid filter name, it will return that filter’s callback function. It can be helpful when aliasing a filter to a different name, using a filter inside of your own filter, or using a filter inside of a shortcode.
module.exports = function(eleventyConfig) {
eleventyConfig.addShortcode("myCustomImage", function(url, alt) {
return `<img src="${eleventyConfig.getFilter("url")(url)}" alt="${alt}">`;
});
};
Asynchronous Filters Added in v2.0.0 Jump to heading
Eleventy has added a new universal filter API for asynchronous filters and extended the currently available addFilter
method to be async-friendly. Note that even though Handlebars is used for synchronous filters in addFilter
, it is excluded from asynchronous filters because Handlebars is not async-friendly.
module.exports = function(eleventyConfig) {
// Async universal filters add to:
// * Liquid
// * Nunjucks
// * JavaScript
eleventyConfig.addFilter("myFilter", async function(value) {
// do some Async work
return value;
});
eleventyConfig.addAsyncFilter("myFilter", async function(value) {
// do some Async work
return value;
});
};
Scoped Data in Filters Jump to heading
A few Eleventy-specific data properties are available to filter callbacks.
this.page
Added in v2.0.0this.eleventy
Added in v2.0.0
module.exports = function(eleventyConfig) {
// Make sure you’re not using an arrow function here: () => {}
eleventyConfig.addFilter("myFilter", function() {
// this.page
// this.eleventy
});
};
Per-Engine filters Jump to heading
Filters can also be specified individually for one or more template engines. (The addFilter
function is actually an alias for calling all of these functions.)
module.exports = function(eleventyConfig) {
// Liquid Filter (async-friendly)
eleventyConfig.addLiquidFilter("myFilter", async function(value) { … });
// Nunjucks Filter
eleventyConfig.addNunjucksFilter("myFilter", function(value) { … });
// Nunjucks Async Filter
// Read the Nunjucks docs before using this (link below)
eleventyConfig.addNunjucksAsyncFilter("myFilter", function() { … });
// Handlebars Filter (no async support)
eleventyConfig.addHandlebarsHelper("myFilter", function(value) { … });
// JavaScript Template Function (async-friendly)
eleventyConfig.addJavaScriptFunction("myFilter", async function(value) { … });
};
Note that Nunjucks addNunjucksAsyncFilter
requires the use of callbacks for async behavior. Make sure you read up on it!
From the Community Jump to heading
×33 resources courtesy of 11tybundle.dev curated by Bob Monsour