- Stable
3.0.0
Toggle Menu
5.81s
40.14s
Global Data Files
Global data is data that is exposed to every template in an Eleventy project.
One way to create global data is through global data files: JSON and JavaScript files placed inside of the global data folder. The global data folder is placed inside the project's input directory (set by the dir.input
configuration option), and the name of the global data folder is set by the dir.data
configuration option (_data
by default). All *.json
and module.exports
values from *.js
files in this directory will be added into a global data object available to all templates.
You may also be interested in config global data, which is another way to add global data to every template in an Eleventy project.
示例
假设有一个 JSON 文件位于 _data/userList.json
路径,其内容如下:
["user1", "user2"];
你就可以在模板中通过 userList
键来访问以上数据了,如下所示:
{
userList: ["user1", "user2"];
}
Folders
如果数据文件放在了子目录下,则该目录的名称将成为全局数据对象结构中的一个层级。例如,在前面示例中,我们假设 userList.json
文件被放到了 users
目录下,其完整路径为 _data/users/userList.json
。
我们在模板中再访问该数据时,该数据就位于 users
键下了,如下所示:
{
users: {
userList: ["user1", "user2"];
}
}
使用 JavaScript 替代 JSON
关于通过 module.exports
从任意 JavaScript 数据文件(JavaScript data files)中导出值的更多信息,请参考 JavaScript 数据文件。
数据源
When the data is merged in the Eleventy Data Cascade, the order of priority for sources of data is (from highest priority to lowest):
- Computed Data
- Front Matter Data in a Template
- Template Data Files
- Directory Data Files (and ascending Parent Directories)
- Front Matter Data in Layouts (this moved in 1.0)
- Configuration API Global Data
- Global Data Files ⬅
From the Community
×53 resources courtesy of 11tybundle.dev curated by Bob Monsour
Expand to see 48 more resources.
Other pages in Data Cascade:
- Front Matter Data
- Template & Directory Data Files
- 全局数据文件
- Data Deep Merge
- Config Global Data
- Computed Data
- Validate Data