Eleventy(11ty) logo Eleventy
The possum is Eleventy’s mascot
Eleventy 中文文档
Menu
Eleventy 5.81s
Nuxt 14.77s

全局数据文件(Global Data Files)

Contents

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 dataAdded in v1.0.0, which is another way to add global data to every template in an Eleventy project.

示例 Jump to heading

假设有一个 JSON 文件位于 _data/userList.json 路径,其内容如下:

[
"user1",
"user2"
]

你就可以在模板中通过 userList 键来访问以上数据了,如下所示:

{
userList: [
"user1",
"user2"
]
}

Folders Jump to heading

如果数据文件放在了子目录下,则该目录的名称将成为全局数据对象结构中的一个层级。例如,在前面示例中,我们假设 userList.json 文件被放到了 users 目录下,其完整路径为 _data/users/userList.json

我们在模板中再访问该数据时,该数据就位于 users 键下了,如下所示:

{
users: {
userList: [
"user1",
"user2"
]
}
}

使用 JavaScript 替代 JSON Jump to heading

关于通过 module.exports 从任意 JavaScript 数据文件(JavaScript data files)中导出值的更多信息,请参考 JavaScript 数据文件

数据源 Jump to heading

When the data is merged in the Eleventy Data Cascade, the order of priority for sources of data is (from highest priority to lowest):

  1. Computed Data
  2. Front Matter Data in a Template
  3. Template Data Files
  4. Directory Data Files (and ascending Parent Directories)
  5. Front Matter Data in Layouts (this moved in 1.0)
  6. Configuration API Global Data
  7. Global Data Files

From the Community Jump to heading

×42 resources courtesy of 11tybundle.dev curated by IndieWeb Avatar for https://www.bobmonsour.com/Bob Monsour

Expand to see 37 more resources.

Other pages in Data Cascade:


相关文档