get_address.html 1.6 KB

1234567891011121314151617181920212223
  1. {{/* Function to return a formatted address given a semantic address. */}}
  2. {{/* Check for valid site config. */}}
  3. {{ if not site.Data.address_formats }}{{errorf "Address formats missing from `data/address_formats.toml`!"}}{{end}}
  4. {{ $page := . }}
  5. {{ $address := .address }}
  6. {{ $format_name := $page.Params.address_format | default site.Params.locale.address_format | default "en-us" }}
  7. {{ if not (isset site.Data.address_formats $format_name) }}{{ errorf "Address format `%s` missing from `data/address_formats.toml`!" $format_name }}{{end}}
  8. {{ $format := index site.Data.address_formats $format_name }}
  9. {{ $address_display := slice }}
  10. {{ range $k, $v := $format.order }}
  11. {{ if eq $v "street" | and $address.street }}{{$address_display = $address_display | append $address.street | append (index $format.delimiters $k | default "") }}{{end}}
  12. {{ if eq $v "city" | and $address.city }}{{$address_display = $address_display | append $address.city | append (index $format.delimiters $k | default "") }}{{end}}
  13. {{ if eq $v "region" | and $address.region }}{{$address_display = $address_display | append $address.region | append (index $format.delimiters $k | default "") }}{{end}}
  14. {{ if eq $v "postcode" | and $address.postcode }}{{$address_display = $address_display | append $address.postcode | append (index $format.delimiters $k | default "") }}{{end}}
  15. {{ if eq $v "country" | and $address.country }}{{$address_display = $address_display | append $address.country | append (index $format.delimiters $k | default "") }}{{end}}
  16. {{end}}
  17. {{ return (delimit $address_display "") }}