| 1 |
--- |
| 2 |
title: Data sources |
| 3 |
description: Use local and remote data sources to augment or create content. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
aliases: [/extras/datafiles/,/extras/datadrivencontent/,/doc/datafiles/,/templates/data-templates/] |
| 7 |
--- |
| 8 |
|
| 9 |
Hugo can access and [unmarshal](g) local and remote data sources including CSV, JSON, TOML, YAML, and XML. Use this data to augment existing content or to create new content. |
| 10 |
|
| 11 |
A data source might be a file in the `data` directory, a [global resource](g), a [page resource](g), or a [remote resource](g). |
| 12 |
|
| 13 |
## Data directory |
| 14 |
|
| 15 |
The `data` directory in the root of your project may contain one or more data files, in either a flat or nested tree. Hugo merges the data files to create a single data structure, accessible with the `Data` method on a `Site` object. |
| 16 |
|
| 17 |
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. |
| 18 |
|
| 19 |
> [!NOTE] |
| 20 |
> 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. |
| 21 |
|
| 22 |
Theme and module authors may wish to namespace their data files to prevent collisions. For example: |
| 23 |
|
| 24 |
```tree |
| 25 |
project/ |
| 26 |
└── data/ |
| 27 |
└── mytheme/ |
| 28 |
└── foo.json |
| 29 |
``` |
| 30 |
|
| 31 |
> [!NOTE] |
| 32 |
> Do not place CSV files in the `data` directory. Access CSV files as page, global, or remote resources. |
| 33 |
|
| 34 |
See the documentation for the [`Data`][] method on a `Site` object for details and examples. |
| 35 |
|
| 36 |
## Global resources |
| 37 |
|
| 38 |
Use the `resources.Get` and `transform.Unmarshal` functions to access data files that exist as global resources. |
| 39 |
|
| 40 |
See the [`transform.Unmarshal`][global-resource] documentation for details and examples. |
| 41 |
|
| 42 |
## Page resources |
| 43 |
|
| 44 |
Use the `Resources.Get` method on a `Page` object combined with the `transform.Unmarshal` function to access data files that exist as page resources. |
| 45 |
|
| 46 |
See the [`transform.Unmarshal`][page-resource] documentation for details and examples. |
| 47 |
|
| 48 |
## Remote resources |
| 49 |
|
| 50 |
Use the `resources.GetRemote` and `transform.Unmarshal` functions to access remote data. |
| 51 |
|
| 52 |
See the [`transform.Unmarshal`][remote-resource] documentation for details and examples. |
| 53 |
|
| 54 |
## Augment existing content |
| 55 |
|
| 56 |
Use data sources to augment existing content. For example, create a shortcode to render an HTML table from a global CSV resource. |
| 57 |
|
| 58 |
```csv {file="assets/pets.csv"} |
| 59 |
"name","type","breed","age" |
| 60 |
"Spot","dog","Collie","3" |
| 61 |
"Felix","cat","Malicious","7" |
| 62 |
``` |
| 63 |
|
| 64 |
```md {file="content/example.md"} |
| 65 |
{{</* csv-to-table "pets.csv" */>}} |
| 66 |
``` |
| 67 |
|
| 68 |
```go-html-template {file="layouts/_shortcodes/csv-to-table.html"} |
| 69 |
{{ with $file := .Get 0 }} |
| 70 |
{{ with resources.Get $file }} |
| 71 |
{{ with . | transform.Unmarshal }} |
| 72 |
<table> |
| 73 |
<thead> |
| 74 |
<tr> |
| 75 |
{{ range index . 0 }} |
| 76 |
<th>{{ . }}</th> |
| 77 |
{{ end }} |
| 78 |
</tr> |
| 79 |
</thead> |
| 80 |
<tbody> |
| 81 |
{{ range after 1 . }} |
| 82 |
<tr> |
| 83 |
{{ range . }} |
| 84 |
<td>{{ . }}</td> |
| 85 |
{{ end }} |
| 86 |
</tr> |
| 87 |
{{ end }} |
| 88 |
</tbody> |
| 89 |
</table> |
| 90 |
{{ end }} |
| 91 |
{{ else }} |
| 92 |
{{ errorf "The %q shortcode was unable to find %s. See %s" $.Name $file $.Position }} |
| 93 |
{{ end }} |
| 94 |
{{ else }} |
| 95 |
{{ errorf "The %q shortcode requires one positional argument, the path to the CSV file relative to the assets directory. See %s" .Name .Position }} |
| 96 |
{{ end }} |
| 97 |
``` |
| 98 |
|
| 99 |
Hugo renders this to: |
| 100 |
|
| 101 |
name|type|breed|age |
| 102 |
:--|:--|:--|:-- |
| 103 |
Spot|dog|Collie|3 |
| 104 |
Felix|cat|Malicious|7 |
| 105 |
|
| 106 |
## Create new content |
| 107 |
|
| 108 |
Use [content adapters][] to create new content. |
| 109 |
|
| 110 |
[`Data`]: /methods/site/data/ |
| 111 |
[content adapters]: /content-management/content-adapters/ |
| 112 |
[global-resource]: /functions/transform/unmarshal/#global-resource |
| 113 |
[page-resource]: /functions/transform/unmarshal/#page-resource |
| 114 |
[remote-resource]: /functions/transform/unmarshal/#remote-resource |