Skip to content

Instantly share code, notes, and snippets.

@budparr
Created April 14, 2019 18:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save budparr/25ba0f25156de502006386881dd12770 to your computer and use it in GitHub Desktop.
Save budparr/25ba0f25156de502006386881dd12770 to your computer and use it in GitHub Desktop.
Fooling around with incorporating Airtable data into a Hugo site using getJSON. Started with some code from https://github.com/colinbate/for-sale/ and first just wanted to pull in data from another table.
{{ $apiPrefix := "https://api.airtable.com/v0/"}}
{{ $key := getenv "AIRTABLE_KEY" }}
{{ if isset .Site.Params "view" }}
{{ .Scratch.Set "view" (printf "&view=%s" .Site.Params.view) }}
{{ else }}
{{ .Scratch.Set "view" "" }}
{{ end }}
{{ if isset .Site.Params "speakers_view" }}
{{ .Scratch.Set "speakers_view" (printf "&view=%s" .Site.Params.speakers_view) }}
{{ else }}
{{ .Scratch.Set "speakers_view" "" }}
{{ end }}
{{ $results := getJSON $apiPrefix $.Site.Params.base "/" $.Site.Params.table "?api_key=" $key (.Scratch.Get "view")}}
{{ $speakers := getJSON $apiPrefix $.Site.Params.base "/" $.Site.Params.speakers "?api_key=" $key (.Scratch.Get "speakers_view")}}
{{ range $results.records }}
{{ with .fields.Name }}
<h1>{{ . }}</h1>
{{end}}
{{ with .fields.Speakers }}
{{ range . }}
{{ $speakers_match := where ($speakers.records) "id" (string .) }}
{{ range $speakers_match }}
{{ with .fields.Name }}
<h2>{{ . }} </h2>
{{ end }}
{{ end }}
{{ end }}
{{end}}
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment