Skip to content
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

Closed
danawoodman opened this issue Jan 6, 2024 · 4 comments
Closed

generator: allow arbitrary go code in templates #399

danawoodman opened this issue Jan 6, 2024 · 4 comments
Labels
enhancement New feature or request generator NeedsFix Needs implementing parser

Comments

@danawoodman
Copy link

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:

package views

templ ItemDetail(item Item) {
	t := item.CreatedAt.Local()
	<section>
		<h1>{ item.Name }</h1>
		<time datetime={ t.Format(time.RFC3339) }>{ t.Format("Jan 2, 2006") }</time>
	</section>
}

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!

@dougbarrett
Copy link
Contributor

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 tools.templ file that I can easily throw small helpers into like this - it helps to keep the actual templates as lean as possible

@danawoodman
Copy link
Author

def works for some scenarios!

still think some intermediary variable creation would be a valuable addition 😇

@a-h
Copy link
Owner

a-h commented Jan 6, 2024

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. fmt.Sprintf(), time.Now().Format()etc.

@joerdav joerdav added enhancement New feature or request parser generator NeedsFix Needs implementing labels Jan 30, 2024
@joerdav joerdav changed the title Feature request: ability to define temporary variables generator: allow arbitrary go code in templates Jan 30, 2024
@joerdav
Copy link
Collaborator

joerdav commented Jan 31, 2024

Just spotted an older ticket which this will be tracked in. #109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request generator NeedsFix Needs implementing parser
Projects
None yet
Development

No branches or pull requests

4 participants