package templates import "fmt" import "slices" type PageData struct { Balance float32 Expenses []float32 TotalThisMonth float32 PercentComparedToLastMonth float32 } templ myBalanceComponent(balance float32) {
My balance

${ fmt.Sprintf("%.2f", balance) }

App logo: a white circle overlapping from the left a filled in black circle
} var days []string = []string{"mon", "tue", "wed", "thu", "fri", "sat", "sun"} // returns templ SummaryComponent // with all attributes computed from the expenses slice // the percentages of the columns, current day number, etc // NOTE: this seems to be the way to mix go calculations and templates func prepareSummaryComponent(expenses []float32, totalThisMonth, percentComparedToLastMonth float32) templ.Component { fmt.Println("hello, preparing expenses: ", expenses) max := slices.Max(expenses) percentages := make([]float32, 0, len(expenses)) for _, price := range expenses { percentages = append(percentages, price/max) } currentDayNum := 2 return spendingSummaryComponent(expenses, percentages, currentDayNum, percentComparedToLastMonth, totalThisMonth) } css expenseBarVars(percentage float32) { --height-percentage: { fmt.Sprintf("%.2f", percentage) }; height: calc(var(--height-percentage) * 8rem); } templ dayExpenseColumn(expense, percentage float32, day string, isCurrentDay bool) {
${ fmt.Sprintf("%.2f", expense ) }

{ day }

} // The 7 vertical bars of the per-day expenses templ expensesChart(expenses, percentages []float32, currentDayNum int) {
for i := 0; i < 7; i++ { @dayExpenseColumn(expenses[i], percentages[i], days[i], currentDayNum == i) }
} // Big container with chard and total expenses of the week templ spendingSummaryComponent(expenses, percentages []float32, currentDayNum int, totalThisMonth, percentComparedToLastMonth float32) {
Spending - Last 7 days @expensesChart(expenses, percentages, currentDayNum)

Total this month

$ { fmt.Sprintf("%.2f", totalThisMonth) }

{ fmt.Sprintf("%.2f", percentComparedToLastMonth) }%

from last month
} templ WidgetsComponend(pageData PageData) {
@myBalanceComponent(pageData.Balance) @prepareSummaryComponent(pageData.Expenses, pageData.PercentComparedToLastMonth, pageData.TotalThisMonth)
} templ IndexPage(pageData PageData) { Frontend Mentor | Expenses chart component
@WidgetsComponend(pageData)
Challenge by Frontend Mentor. Coded by Your Name Here.
}