-
-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
generator: allow arbitrary go code in templates #399
Comments
As a possible alternative, you may want to consider doing: package views
func ltf(i Item, f string) time.Time {
return i.CreatedAt.Local().Format(f)
}
templ ItemDetail(i Item) {
<section>
<h1>{ i.Name }</h1>
<time datetime={ ltf(i, time.RFC3339)) }>{ ltf(i, "Jan 2, 2006") }</time>
</section>
} i'm finding that I'm often making a |
def works for some scenarios! still think some intermediary variable creation would be a valuable addition 😇 |
There's a plan to add the ability to introduce arbitrary code. The discussion around it is in #109 (comment) The design will looks like this: {{ x := "variable" }} It's not much work to add it. There's already an expression parser that looks to match start and end braces, and is aware of start and ends of strings, comments etc. Adding lots of code into templates isn't something I'd recommend though. I prefer more of a View Model type approach, where you can keep the logic for creating the view separate from the rendering part (as per https://templ.guide/core-concepts/view-models), but there are places where defining a variable or adding some extra code can be useful. Having a set of helpers like @dougbarrett suggests is a good idea. Might need to mention some of the common string format operations that there are in Go in the documentation, e.g. |
Just spotted an older ticket which this will be tracked in. #109 |
It would be awesome if you could define a temporary variable somehow (this is just one possible suggestion) in cases where you might be duplicating lots of code to reuse something, like this:
Unless I'm mistaken, this doesn't appear to be supported - so if I'm correct - I'd love to see this implemented some day!
The text was updated successfully, but these errors were encountered: