Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 3.05 KB

File metadata and controls

53 lines (40 loc) · 3.05 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" %}

{% tableofcontents %}

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

  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.

Examples

Apply a default layout to multiple templates

{% codetitle "posts/posts.json" %} {%- set codeBlock %}{% raw %} { "layout": "layouts/post.njk" } {% endraw %}{%- endset %} {{ codeBlock | highlight("json") | safe }}

Using the above in posts/posts.json will configure a layout for all of the templates inside of posts/*.

Additional Customizations

Sources of Data

{% include "datasources.md" %}