mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-18 21:11:19 +02:00
Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
---
|
||||
title: Data sources
|
||||
description: Use local and remote data sources to augment or create content.
|
||||
categories: [content management]
|
||||
keywords: [data,json,toml,yaml,xml]
|
||||
menu:
|
||||
docs:
|
||||
parent: content-management
|
||||
weight: 280
|
||||
weight: 280
|
||||
toc: true
|
||||
categories: []
|
||||
keywords: []
|
||||
aliases: [/extras/datafiles/,/extras/datadrivencontent/,/doc/datafiles/,/templates/data-templates/]
|
||||
---
|
||||
|
||||
@@ -22,9 +16,8 @@ The `data` directory in the root of your project may contain one or more data fi
|
||||
|
||||
Hugo also merges data directories from themes and modules into this single data structure, where the `data` directory in the root of your project takes precedence.
|
||||
|
||||
{{% note %}}
|
||||
Hugo reads the combined data structure into memory and keeps it there for the entire build. For data that is infrequently accessed, use global or page resources instead.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Hugo reads the combined data structure into memory and keeps it there for the entire build. For data that is infrequently accessed, use global or page resources instead.
|
||||
|
||||
Theme and module authors may wish to namespace their data files to prevent collisions. For example:
|
||||
|
||||
@@ -35,14 +28,11 @@ project/
|
||||
└── foo.json
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
Do not place CSV files in the `data` directory. Access CSV files as page, global, or remote resources.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Do not place CSV files in the `data` directory. Access CSV files as page, global, or remote resources.
|
||||
|
||||
See the documentation for the [`Data`] method on a `Site` object for details and examples.
|
||||
|
||||
[`Data`]: /methods/site/data/
|
||||
|
||||
## Global resources
|
||||
|
||||
Use the `resources.Get` and `transform.Unmarshal` functions to access data files that exist as global resources.
|
||||
@@ -65,17 +55,17 @@ See the [`transform.Unmarshal`](/functions/transform/unmarshal/#remote-resource)
|
||||
|
||||
Use data sources to augment existing content. For example, create a shortcode to render an HTML table from a global CSV resource.
|
||||
|
||||
{{< code file=assets/pets.csv >}}
|
||||
```csv {file="assets/pets.csv"}
|
||||
"name","type","breed","age"
|
||||
"Spot","dog","Collie","3"
|
||||
"Felix","cat","Malicious","7"
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=content/example.md lang=text >}}
|
||||
```text {file="content/example.md"}
|
||||
{{</* csv-to-table "pets.csv" */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=layouts/shortcodes/csv-to-table.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/csv-to-table.html"}
|
||||
{{ with $file := .Get 0 }}
|
||||
{{ with resources.Get $file }}
|
||||
{{ with . | transform.Unmarshal }}
|
||||
@@ -104,7 +94,7 @@ Use data sources to augment existing content. For example, create a shortcode to
|
||||
{{ else }}
|
||||
{{ errorf "The %q shortcode requires one positional argument, the path to the CSV file relative to the assets directory. See %s" .Name .Position }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Hugo renders this to:
|
||||
|
||||
@@ -117,4 +107,5 @@ Felix|cat|Malicious|7
|
||||
|
||||
Use [content adapters] to create new content.
|
||||
|
||||
[`Data`]: /methods/site/data/
|
||||
[content adapters]: /content-management/content-adapters/
|
||||
|
Reference in New Issue
Block a user