Replies: 2 comments 1 reply
-
I would separate this into two problems. First, get the data into a format that is structured in a similar way that you want to display it in your UI, and then, display the data. I think that having complex logic in views is hard to unit test and debug, and that splitting the problem of data arrangement and display is a good approach, despite the extra work of creating a view model and mapping data. This idea is expressed in a short page at https://templ.guide/core-concepts/view-models |
Beta Was this translation helpful? Give feedback.
-
Oh, I should also say that there is a plan to introduce arbitrary code blocks into templ, see #399 (comment) |
Beta Was this translation helpful? Give feedback.
-
Hello, guys!
I couldn't find in docs, is it possible to modify data in templates?
Short example: suppose I want to render a tree-like structure recursively, let's say a representation of folders and files, and I want to numerate each file across the whole tree (not its own counter within each individual subfolder), the logical solution here is to modify the counter upon each file rendered and pass to the recursive calls.
The question: how to modify that counter in template?
Ofc, I found workarounds, such as a declaration of a global variable and a function that modifies the value and return a new, modified value; then I pass that function into the recursive calls.
But honestly, it's not the most elegant code...
var counter = -1
func increment() int {
counter++
return counter
}
Beta Was this translation helpful? Give feedback.
All reactions