- Stable
3.0.0
Toggle Menu
Eleventy
5.81s
Gatsby
43.36s
Template and Directory Specific Data Files
Contents
While you can provide global data files to supply data to all of your templates, you may want some of your data to be available locally only to one specific template or to a directory of templates. For that use, we also search for JSON and JavaScript Data Files in specific places in your directory structure.
For example, consider a template located at posts/subdir/my-first-blog-post.md
. Eleventy will look for data in the following places (starting with highest priority, local data keys override global data):
- Content Template Front Matter Data
- merged with any Layout Front Matter Data
- Template Data File (data is only applied to
posts/subdir/my-first-blog-post.md
)posts/subdir/my-first-blog-post.11tydata.js
JavaScript Data Filesposts/subdir/my-first-blog-post.11tydata.json
posts/subdir/my-first-blog-post.json
- Directory Data File (data applies to all templates in
posts/subdir/*
)posts/subdir/subdir.11tydata.js
JavaScript Data Filesposts/subdir/subdir.11tydata.json
posts/subdir/subdir.json
- Parent Directory Data File (data applies to all templates in
posts/**/*
, including subdirectories)posts/posts.11tydata.js
JavaScript Data Filesposts/posts.11tydata.json
posts/posts.json
- Global Data Files in
_data/*
(.js
or.json
files) available to all templates.
Examples
Apply a default layout to multiple templates
Filename posts/posts.json
{ "layout": "layouts/post.njk" }
Using the above in posts/posts.json
will configure a layout for all of the templates inside of posts/*
.
Additional Customizations
- The name of the data file must match either the post or the directory it resides within. You can change this behavior using the
setDataFileBaseName
method in the Configuration API. - You can use the
setDataFileSuffixes
Configuration API method to customize the default file suffixes or disable this feature altogether. - Note that any Custom Formats specified in your configuration will also be taken into account at a lower priority than their JavaScript or JSON counterparts.
Sources of Data
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
- Template Data Files ⬅
- Directory Data Files (and ascending Parent Directories) ⬅
- Front Matter Data in Layouts (this moved in 1.0)
- Configuration API Global Data
- Global Data Files