mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'
This commit is contained in:
@@ -3,25 +3,16 @@ title: Data
|
||||
description: Returns a data structure composed from the files in the data directory.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
related:
|
||||
- functions/collections/IndexFunction
|
||||
- functions/transform/Unmarshal
|
||||
- functions/collections/Where
|
||||
- functions/collections/Sort
|
||||
returnType: map
|
||||
signatures: [SITE.Data]
|
||||
params:
|
||||
functions_and_methods:
|
||||
returnType: map
|
||||
signatures: [SITE.Data]
|
||||
---
|
||||
|
||||
Use the `Data` method on a `Site` object to access data within the `data` directory, or within any directory [mounted] to the `data` directory. Supported data formats include JSON, TOML, YAML, and XML.
|
||||
|
||||
[mounted]: /hugo-modules/configuration/#module-configuration-mounts
|
||||
|
||||
{{% note %}}
|
||||
Although Hugo can unmarshal CSV files with the [`transform.Unmarshal`] function, do not place CSV files in the `data` directory. You cannot access data within CSV files using this method.
|
||||
|
||||
[`transform.Unmarshal`]: /functions/transform/unmarshal/
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Although Hugo can unmarshal CSV files with the [`transform.Unmarshal`] function, do not place CSV files in the `data` directory. You cannot access data within CSV files using this method.
|
||||
|
||||
Consider this `data` directory:
|
||||
|
||||
@@ -37,23 +28,23 @@ data/
|
||||
|
||||
And these data files:
|
||||
|
||||
{{< code file=data/books/fiction.yaml lang=yaml >}}
|
||||
```yaml {file="data/books/fiction.yaml"}
|
||||
- title: The Hunchback of Notre Dame
|
||||
author: Victor Hugo
|
||||
isbn: 978-0140443530
|
||||
- title: Les Misérables
|
||||
author: Victor Hugo
|
||||
isbn: 978-0451419439
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=data/books/nonfiction.yaml lang=yaml >}}
|
||||
```yaml {file="data/books/nonfiction.yaml"}
|
||||
- title: The Ancien Régime and the Revolution
|
||||
author: Alexis de Tocqueville
|
||||
isbn: 978-0141441641
|
||||
- title: Interpreting the French Revolution
|
||||
author: François Furet
|
||||
isbn: 978-0521280495
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Access the data by [chaining](g) the [identifiers](g):
|
||||
|
||||
@@ -108,3 +99,5 @@ In the template examples above, each of the keys is a valid identifier. For exam
|
||||
```
|
||||
|
||||
[`index`]: /functions/collections/indexfunction/
|
||||
[`transform.Unmarshal`]: /functions/transform/unmarshal/
|
||||
[mounted]: /configuration/module/#mounts
|
||||
|
Reference in New Issue
Block a user