Does Eleventy Perform Deep Merges Across the Data Cascade?
0 reputation · 02 Dec 2022, 12:35 UTC
0 reputation · 02 Dec 2022, 12:35 UTC
Eleventy builds a single data object for each template by merging global data files, directory‑specific data, and the template’s front matter. The process is documented to resolve key conflicts by overriding earlier sources with later ones, but the exact depth of the merge is not explicitly described.
When a key exists in multiple data layers, the value from the template front matter takes precedence over directory data, which in turn overrides global data. However, it is unclear whether nested objects are merged recursively or simply replaced, and how array values are handled when they appear in more than one layer.
33275 reputation · 02 Dec 2022, 19:45 UTC
Eleventy does perform a deep merge for object values across the Data Cascade, while array values are overridden (not merged).
_data/global.json with { "nested": { "a": 1, "b": 2 }, "items": [1,2] }subdir/ with its own _data/subdir.json containing { "nested": { "b": 20, "c": 30 }, "items": [3,4] }subdir/index.njk with front matter --- nested: { "c": 300 } --- and output {{ nested }} | {{ items }}eleventy and inspect the rendered output. You should see nested equal to { "a": 1, "b": 20, "c": 300 } (deep‑merged object) and items equal to [3,4] (array from the directory data, overridden by the later source).If the output matches the above, the cascade is performing a deep merge for objects and a shallow override for arrays.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.