Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 3.07 KB

data-template-dir.md

File metadata and controls

46 lines (34 loc) · 3.07 KB
eleventyNavigation
parent key title order
Data Cascade
Template and Directory Data Files
Template & Directory Data Files
2

Template and Directory Specific Data Files {% addedin "0.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.

{% callout "info" %}Important exception: Template and Directory Specific Data Files are not preprocessed through a templating engine. Global Data files are.{% endcallout %}

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):

  1. Content Template Front Matter Data
  2. Template Data File (data is only applied to posts/subdir/my-first-blog-post.md)
    • posts/subdir/my-first-blog-post.11tydata.js {% minilink "/docs/data-js" %}{% addedin "0.5.3" %}JavaScript Data Files{% endminilink %}
    • posts/subdir/my-first-blog-post.11tydata.json {% addedin "0.5.3" %}
    • posts/subdir/my-first-blog-post.json
  3. Directory Data File (data applies to all templates in posts/subdir/*)
    • posts/subdir/subdir.11tydata.js {% minilink "/docs/data-js" %}{% addedin "0.5.3" %}JavaScript Data Files{% endminilink %}
    • posts/subdir/subdir.11tydata.json {% addedin "0.5.3" %}
    • posts/subdir/subdir.json
  4. Parent Directory Data File (data applies to all templates in posts/**/*, including subdirectories) {% addedin "0.2.15" %}
    • posts/posts.11tydata.js {% minilink "/docs/data-js" %}{% addedin "0.5.3" %}JavaScript Data Files{% endminilink %}
    • posts/posts.11tydata.json {% addedin "0.5.3" %}
    • posts/posts.json
  5. Global Data Files in _data/* (.js or .json files) available to all templates.

{% callout "info" %}Note that the name of the data file must match either the post or the directory it resides within.{% endcallout %}

Note that any Custom Formats {% addedin "0.10.0" %} specified in your configuration will also be taken into account at a lower priority than their JavaScript or JSON counterparts.

Change the .11tydata.js file suffix {% addedin "0.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

{% include "datasources.md" %}