[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to navigation Skip to main content
Eleventy
Eleventy Documentation
Stable
3.0.0
Toggle Menu
Eleventy 1.93s
Next.js 70.65s

Data Deep Merge

Contents

Use a full deep merge when combining the Data Cascade. This will use something similar to lodash.mergewith to combine Arrays and deep merge Objects, rather than a simple top-level merge using Object.assign.

Read more at Issue #147. As of Eleventy 1.0 this defaults to enabled (but API still exists for opt-out).

eleventy.config.js
export default function (eleventyConfig) {
// defaults to true in 1.0, use false to opt-out
eleventyConfig.setDataDeepMerge(false);

// requires opt-in for 0.x
eleventyConfig.setDataDeepMerge(true);
};
module.exports = function (eleventyConfig) {
// defaults to true in 1.0, use false to opt-out
eleventyConfig.setDataDeepMerge(false);

// requires opt-in for 0.x
eleventyConfig.setDataDeepMerge(true);
};

Note that all data stored in the pagination variable is exempted from this behavior (we don’t want pagination.items to be merged together).

Example

Filename my-template.md
---
title: This is a Good Blog Post
tags:
- CSS
- HTML
layout: my-layout.njk
eleventyNavigation:
key: my-key
---
Filename _includes/my-layout.njk
---
title: This is a Very Good Blog Post
author: Zach
tags:
- JavaScript
eleventyNavigation:
parent: test
---

Without Deep Data Merge

Results in the following data available in my-template.md:

Syntax JavaScript
{
"title": "This is a Good Blog Post",
"author": "Zach",
"tags": ["CSS", "HTML"],
"eleventyNavigation": {
"key": "my-key"
}
}

With Data Deep Merge

With this enabled, your data structure will look like this when my-template.md is rendered:

Syntax JavaScript
{
"title": "This is a Good Blog Post",
"author": "Zach",
"tags": ["CSS", "HTML", "JavaScript"],
"eleventyNavigation": {
"key": "my-key",
"parent": "test"
}
}

Using the override: prefix

Use the override: prefix on any data key to opt-out of this merge behavior for specific values or nested values.

Filename posts/posts.json
{
"tags": ["posts"]
}
Filename posts/firstpost.md
---
override:tags: []
---

Even though normally the posts/firstpost.md file would inherit the posts tag from the directory data file (per normal data cascade rules), we can override the tags value to be an empty array to opt-out of this behavior.


Other pages in Data Cascade: