From f952caf3f633cb6bebddea381d5184b5dcb25165 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 6 Aug 2024 20:52:46 +0200 Subject: [PATCH] Creating a formatCurrency function --- cmd/web/render.go | 9 ++++++++- cmd/web/templates/buy-once.page.gohtml | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/web/render.go b/cmd/web/render.go index 5316472..1c73cb6 100644 --- a/cmd/web/render.go +++ b/cmd/web/render.go @@ -22,7 +22,14 @@ type templateData struct { IsAuthenticated int } -var functions = template.FuncMap{} +var functions = template.FuncMap{ + "formatCurrency": formatCurrency, +} + +func formatCurrency(n int) string { + f := float32(n / 100) + return fmt.Sprintf("€%.2f", f) +} //go:embed templates var templateFS embed.FS diff --git a/cmd/web/templates/buy-once.page.gohtml b/cmd/web/templates/buy-once.page.gohtml index 4b18ff4..21785d1 100644 --- a/cmd/web/templates/buy-once.page.gohtml +++ b/cmd/web/templates/buy-once.page.gohtml @@ -19,8 +19,8 @@ Buy one widget autocomplete="off" novalidate=""> - -

{{$widget.Name}}: {{$widget.Price}}

+ +

{{$widget.Name}}: {{formatCurrency $widget.Price}}

{{$widget.Description}}