Add docs for Data Files

Fixes #887
This commit is contained in:
bep 2015-02-09 16:52:52 +01:00
parent 6afe70d5da
commit 03fff880ec
4 changed files with 73 additions and 1 deletions

View file

@ -0,0 +1,68 @@
---
aliases:
- /doc/scratch/
date: 2015-01-22
menu:
main:
parent: extras
next: /extras/highlighting
prev: /extras/scratch
title: Data Files
weight: 90
---
In addition to the [built-in variables](/templates/variables/) available from Hugo, you can specify your own custom data that can be accessed via templates or shortcodes.
Hugo supports loading data from [YAML](http://yaml.org/), [JSON](http://www.json.org/), and [TOML](https://github.com/toml-lang/toml) files located in the `data` directory.
**It even works with [LiveReload](/extras/livereload/).**
## The Data Folder
As explained in [Source Organization](/overview/source-directory/), the `data` folder is where you can store additional data for Hugo to use when generating your site. These files must be YAML, JSON or TOML files (using either the `.yml`, `.yaml`, `.json` or `toml` extension) and the data will be accessible as a `map` in `.Site.Data`.
**The keys in this map will be a dot chained set of _path_, _filename_ and _key_ in file (if applicable).**
This is best explained with an example:
## Example: Jaco Pastorius' Solo Discography
[Jaco Pastorius](http://en.wikipedia.org/wiki/Jaco_Pastorius_discography) was a great bass player, but his solo discography is short enough to use as an example.
The example below uses TOML as file format.
Given the file `data/jazz/bass/jacopastorius.toml` with the content below:
```
discography = [
"1974 Modern American Music … Period! The Criteria Sessions",
"1974 Jaco",
"1976 - Jaco Pastorius",
"1981 - Word of Mouth",
"1981 - The Birthday Concert (released in 1995)",
"1982 - Twins I & II (released in 1999)",
"1983 - Invitation",
"1986 - Broadway Blues (released in 1998)",
"1986 - Honestly Solo Live (released in 1990)",
"1986 - Live In Italy (released in 1991)",
"1986 - Heavy'n Jazz (released in 1992)",
"1991 - Live In New York City, Volumes 1-7.",
"1999 - Rare Collection (compilation)",
"2003 - Punk Jazz: The Jaco Pastorius Anthology (compilation)",
"2007 - The Essential Jaco Pastorius (compilation)"
]
```
This list can be accessed via `.Site.Data.jazz.bass.jacopastorius.discography`.
You can now render the list of recordings in a template:
```
<ul>
{{ range $.Site.Data.jazz.bass.jacopastorius.discography }}
<li>{{ . }}</li>
{{ end }}
</ul>
```

View file

@ -5,7 +5,7 @@ date: 2015-01-22
menu:
main:
parent: extras
next: /extras/highlighting
next: /extras/datafiles
prev: /extras/pagination
title: Scratch
weight: 80

View file

@ -20,6 +20,7 @@ The top level of a source directory will typically have the following elements:
▸ archetypes/
▸ content/
▸ data/
▸ layouts/
▸ static/
▸ themes/
@ -28,6 +29,7 @@ The top level of a source directory will typically have the following elements:
Learn more about the different directories and what their purpose is:
* [config](/overview/configuration/)
* [data](/extras/datafiles/)
* [archetypes](/content/archetypes/)
* [content](/content/organization/)
* [layouts](/layout/overview/)
@ -50,6 +52,7 @@ An example directory may look like:
| └── quote
| | ├── first.md
| | └── second.md
├── data
├── layouts
| ├── _default
| | ├── single.html

View file

@ -109,6 +109,7 @@ Also available is `.Site` which has the following:
**.Site.LastChange** A string representing the last time content has been updated.<br>
**.Site.Permalinks** A string to override the default permalink format. Defined in the site configuration.<br>
**.Site.BuildDrafts** A boolean (Default: false) to indicate whether to build drafts. Defined in the site configuration.<br>
**.Site.Data** Custom data, see [Data Files](/extras/datafiles/).<br>
## Hugo Variables