Data Files
Partials ant layouts can pull information from data files.data
folder is a mini read-only DB.
- Create
data/states.json
{
"AL": {
"name": "Alabama",
"capital": "Montgomery",
"lat": "32.361538",
"long": "-86.279118"
},
"AK": {
"name": "Alaska",
"capital": "Juneau",
"lat": "58.301935",
"long": "-134.419740"
}
}
- Loop through data in
layouts/_default/single.html
Access data via.Site.Data.states
{{ define "main" }}
<h3>Using json data file.</h3>
{{ range .Site.Data.states }}
{{ .name }} - {{.capital}} <br />
{{ end }}
{{ end }}
In browser:
Using json data file.
Alaska - Juneau
Alabama - Montgomery