Skip to content

Latest commit

 

History

History
145 lines (112 loc) · 5.68 KB

data-computed.md

File metadata and controls

145 lines (112 loc) · 5.68 KB
eleventyNavigation
parent key order
Data Cascade
Computed Data
5

Computed Data {% addedin "0.11.0" %}

At the end of the Data Cascade you may want to inject Data properties into your data object that are based on other data values. To do that you can use the new eleventyComputed feature.

[[toc]]

Real World Example

Say you want to use Eleventy’s Navigation Plugin to create a navigation menu for your site. This plugin relies on the eleventyNavigation object to be set. You don’t necessarily want to set this object manually in front matter in each individual source file. This is where Computed Data comes in!

Consider a blog post with the following front matter format:

{% codetitle "posts/my-page-title.md" %}

---
title: My Page Title
parent: My Parent Key
---

If this file is generated by a Content Management System (like Netlify CMS, in my particular case), I may not be able to (or want to) create the eleventyNavigation object for each of them. I would also not be able to just dump a standard Data Directory File in there either (that would be useful for setting defaults but we don’t want the same values for every markdown file). eleventyNavigation properties must be set based on other data properties.

Instead, I created this Data Directory File using eleventyComputed:

{% codetitle "posts/posts.11tydata.js" %}

module.exports = {
  eleventyComputed: {
    eleventyNavigation: {
      key: data => data.title,
      parent: data => data.parent
    }
  }
};

{% callout "info" %}If you want to use a JavaScript function for your eleventyComputed properties, you must use either JavaScript front matter or a JavaScript data file (template, directory, or global). YAML and JSON do not support JavaScript functions.{% endcallout %}

The resulting data for each posts/*.md file when processed by Eleventy has the following structure:

{% codetitle "posts/my-page-title.md", "Data Cascade for" %}

{
  "title": "My Page Title",
  "parent": "My Parent Key",
  "eleventyNavigation": {
    "key": "My Page Title",
    "parent": "My Parent Key"
  }
}

Just Use JavaScript

Use any arbitrary JavaScript for an eleventyComputed property. Note that JavaScript functions require either JavaScript front matter or a JavaScript data file (template, directory, or global). YAML and JSON do not support JavaScript functions.

Here’s a bunch of examples:

module.exports = {
  eleventyComputed: {
    myString: "this is just a raw string",
    myFunction: data => `This is a string using ${data.someValue}.`,
    myAsyncFunction: async data => await someAsyncThing(),
    myPromise: data => {
      return new Promise(resolve => {
        setTimeout(() => resolve("100ms DELAYED HELLO"), 100);
      })
    }
  }
};

Use a Template String

If you want to use eleventyComputed in YAML front matter, you can use the template syntax string that matches the syntax of the template.

This is how permalink works, if you’re already familiar with that.

Consider our first example, but using Nunjucks (this example is also valid Liquid syntax).

{% codetitle "posts/my-page-title.njk" %}

{% raw %}

---
title: My Page Title
parent: My Parent Key
eleventyComputed:
  eleventyNavigation:
    key: "{{ title }}"
    parent: "{{ parent }}"
---

{% endraw %}

The above would also resolve to the same Data Cascade:

{% codetitle "posts/my-page-title.njk", "Data Cascade for" %}

{
  "title": "My Page Title",
  "parent": "My Parent Key",
  "eleventyNavigation": {
    "key": "My Page Title",
    "parent": "My Parent Key"
  }
}

{% callout "warn" %}Note that template syntax is definitely slower than the “Just Use JavaScript” methods above.{% endcallout %}

{% callout "info" %}This would also work in JSON data files or any other data file type in the cascade, just keep in mind that the template syntax must match the template syntax that it eventually winds up with in the Data Cascade.{% endcallout %}

Advanced Details

We put a lot of work into making this feature as easy to use as possible. Most of these details shouldn’t matter to you as it should Just Work™. But here’s a few things we thought of already and handle in a good way:

  • You can put eleventyComputed anywhere in the Data Cascade: Front Matter, any Data Files (you could even make an eleventyComputed.js global data file if you wanted to set this for your entire site).
  • Reference any of the existing data properties (including ones created by Eleventy like page).
    • You can use or set permalink in eleventyComputed and it will work (permalink is a top-level special case computed property anyway).
  • You can use a computed property that depends on other computed properties (just reference them like they were any other property data.propName and ⚠️ not data.eleventyComputed.propName)
    • The order of the keys in the object doesn’t matter—we smartly figure out what order these should be computed in.
    • We will let you know if you have circular references (key1 uses on key2 which uses key1 again)
  • You can use a nested object of any depth. It can mix, match, and merge with the standard (non-computed) data. This will always do deep merging (independent of your Data Deep Merge configuration).
  • You can reuse and override properties at the same time. In the following example key will have This Is My Key as its value. {% raw %}
---
key: My Key
eleventyComputed:
  key: "This Is {{key}}"
---

{% endraw %}