Template and Directory Specific Data Files New in v0.2.14 #
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
New in v0.5.3JavaScript Data Filesposts/subdir/my-first-blog-post.11tydata.json
New in v0.5.3posts/subdir/my-first-blog-post.json
- Directory Data File (data applies to all templates in
posts/subdir/*
)posts/subdir/subdir.11tydata.js
New in v0.5.3JavaScript Data Filesposts/subdir/subdir.11tydata.json
New in v0.5.3posts/subdir/subdir.json
- Parent Directory Data File (data applies to all templates in
posts/**/*
, including subdirectories) New in v0.2.15posts/posts.11tydata.js
New in v0.5.3JavaScript Data Filesposts/posts.11tydata.json
New in v0.5.3posts/posts.json
- Global Data Files in
_data/*
(available to all templates)
Change the .11tydata.js
file suffix New in v0.5.3 #
Use the Configuration API to change the file suffix used to search for Eleventy data files.
Example: Apply a default layout to multiple templates #
Try adding { "layout": "layouts/post.njk" }
to posts/posts.json
to configure a layout for all of the templates inside of posts/*
.
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):
- Front Matter Data in a Template
- Front Matter Data in Layouts
- Template Data Files ⬅
- Directory Data Files (and ascending Parent Directories) ⬅
- Global Data Files