improve currency shortcodes

pull/290/head
Septs 2022-02-03 09:52:17 +08:00
parent bca12c8e11
commit d58f82a840
No known key found for this signature in database
GPG Key ID: 6EC3B9D7E6EE828F
1 changed files with 8 additions and 11 deletions

View File

@ -1,14 +1,11 @@
{{- $units := slice "" "k" "M" -}}
{{- $from := .Get "from" -}}
{{- $to := default "CNY" (.Get "to") -}}
{{- $value := float (.Get "value") -}}
{{- with getJSON (printf "https://api.exchangerate-api.com/v4/latest/%s" $from) -}}
<a
class="currency"
role="tooltip"
aria-label="&asymp; {{ lang.FormatCurrency 2 $to (mul $value (index .rates $to)) }}"
data-microtip-position="right"
href="https://google.com/search?q={{ $value }} {{ $from }} {{ $to }}"
>
{{- lang.FormatCurrency 2 $from $value -}}
</a>
{{- end -}}
{{- $unit := math.Floor (div (math.Log $value) (math.Log 1000)) | int -}}
{{- $base := math.Pow 1000 $unit -}}
{{- $formatted := printf "%g%s" (div $value $base) (index $units $unit) -}}
{{- $qs := querify "Amount" $value "From" $from "To" $to -}}
<a class="currency" href="https://www.xe.com/currencyconverter/convert/?{{ $qs | safeURL }}">
{{- $formatted }} {{ $from -}}
</a>