Files
umrechner/hugo/content/_content.gotmpl

148 lines
4.2 KiB
Go Template

{{/* Generischer Content Adapter fuer Einheitsumrechner */}}
{{/* Erzeugt alle from-to Permutationsseiten fuer linear, intermediate und runtime */}}
{{ $pbBase := getenv "POCKETBASE_URL" | default site.Params.pocketbase_url }}
{{ $pbUrl := printf "%s/api/collections/currencies/records" $pbBase }}
{{ $pbOpts := dict "method" "GET" "timeout" "15s" "headers" (dict "Accept" (slice "application/json")) }}
{{ $pbData := dict }}
{{ $pbOk := false }}
{{ with try (resources.GetRemote $pbUrl $pbOpts) }}
{{ with .Err }}
{{ warnf "PocketBase nicht erreichbar: %s" . }}
{{ else with .Value }}
{{ with transform.Unmarshal .Content }}
{{ if .items }}
{{ $pbData = .items }}
{{ $pbOk = true }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ $pbRates := dict }}
{{ $latestUpdate := "" }}
{{ range $record := $pbData }}
{{ $pbRates = merge $pbRates (dict $record.id $record.rate) }}
{{ if or (eq $latestUpdate "") (gt $record.updated $latestUpdate) }}
{{ $latestUpdate = $record.updated }}
{{ end }}
{{ end }}
{{ range $categoryName, $categoryData := site.Data }}
{{ $engine := $categoryData.conversion_engine }}
{{ if not $engine }}{{ continue }}{{ end }}
{{ $categorySlug := $categoryData.slug }}
{{ $units := $categoryData.units }}
{{ if eq $engine "runtime" }}
{{ if not $pbOk }}{{ continue }}{{ end }}
{{ $pbCurrencies := slice }}
{{ range $record := $pbData }}
{{ $pbCurrencies = $pbCurrencies | append $record.id }}
{{ end }}
{{ $availableCurrencies := $pbCurrencies | append "eur" }}
{{ range $fromCode := $availableCurrencies }}
{{ range $toCode := $availableCurrencies }}
{{ if eq $fromCode $toCode }}{{ continue }}{{ end }}
{{ $fromName := $fromCode }}
{{ with index $units $fromCode }}
{{ $fromName = .name }}
{{ end }}
{{ $toName := $toCode }}
{{ with index $units $toCode }}
{{ $toName = .name }}
{{ end }}
{{ $slug := printf "%s-in-%s" $fromCode $toCode }}
{{ $title := printf "%s in %s umrechnen"
$fromName $toName }}
{{ $description := printf
"Konvertieren Sie %s in %s. Einfache und schnelle Umrechnung mit Einheitsumrechner."
$fromName $toName }}
{{ $fromCurrency := $fromCode }}
{{ $toCurrency := $toCode }}
{{ $fromRate := 1 }}
{{ if ne $fromCode "eur" }}
{{ $fromRate = index $pbRates $fromCode }}
{{ end }}
{{ $toRate := 1 }}
{{ if ne $toCode "eur" }}
{{ $toRate = index $pbRates $toCode }}
{{ end }}
{{ $rate := div $toRate $fromRate }}
{{ $params := dict
"category" $categorySlug
"from" $fromCode
"to" $toCode
"from_name" $fromName
"to_name" $toName
"slug" $slug
"description" $description
"engine" "runtime"
"currency_from" $fromCurrency
"currency_to" $toCurrency
"rate" $rate
"rates_updated" $latestUpdate
}}
{{ $page := dict
"kind" "page"
"path" $slug
"title" $title
"type" "conversion"
"params" $params
}}
{{ $.AddPage $page }}
{{ end }}
{{ end }}
{{ else }}
{{ range $fromUnit, $fromData := $units }}
{{ $fromName := $fromData.name }}
{{ range $toUnit, $toData := $units }}
{{ if eq $fromUnit $toUnit }}{{ continue }}{{ end }}
{{ $toName := $toData.name }}
{{ $slug := printf "%s-in-%s"
$fromUnit $toUnit }}
{{ $title := printf "%s in %s umrechnen"
$fromName $toName }}
{{ $description := printf
"Konvertieren Sie %s in %s. Einfache und schnelle Umrechnung mit Einheitsumrechner."
$fromName $toName }}
{{ $params := dict
"category" $categorySlug
"from" $fromUnit
"to" $toUnit
"from_name" $fromName
"to_name" $toName
"slug" $slug
"description" $description
}}
{{ if eq $engine "linear" }}
{{ $params = merge $params (dict
"engine" "linear"
"from_factor" $fromData.factor
"to_factor" $toData.factor
) }}
{{ else if eq $engine "intermediate" }}
{{ $params = merge $params (dict
"engine" "intermediate"
"from_unit" $fromUnit
"to_unit" $toUnit
) }}
{{ end }}
{{ $page := dict
"kind" "page"
"path" $slug
"title" $title
"type" "conversion"
"params" $params
}}
{{ $.AddPage $page }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}