Skip to content

What’s the best way to derive values from props? #639

Answered by joerdav
gabrielvincent asked this question in Q&A
Discussion options

You must be logged in to vote

I like that this separates logic from the template itself, very nice approach.

Depending on your preferences you could also in this example just use a function to transform your model:

// This is a regular function
func totalValue(orders []Order) int {
	totalValue := 0
	for _, order := range orders {
		totalValue += order.Value
	}
	return totalValue
}

templ OrdersPage(orders []Order) {
    <span>Total: ${ strconv.Itoa(totalValue(orders)) }</span>
    <ul>
        for _, order := range orders {
            <li>{order.OrderNumber}</li>
        }
    </ul>
}

There is also this issue which may make things easier for you once resolved: #109

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@joerdav
Comment options

Answer selected by joerdav
@gabrielvincent
Comment options

Comment options

You must be logged in to vote
1 reply
@gabrielvincent
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants