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

feature: add templ.HasChildren function #975

Open
epicbytes opened this issue Oct 30, 2024 · 3 comments
Open

feature: add templ.HasChildren function #975

epicbytes opened this issue Oct 30, 2024 · 3 comments
Labels
enhancement New feature or request NeedsInvestigation Issue needs some investigation before being fixed

Comments

@epicbytes
Copy link

Some components should have a default display if no child is passed, but the problem is that when callinggetChildren(ctx).
But I get NopComponent. This is unacceptable because there is a nil check in the same function. I can't compare and understand whether there are child components or not.

Please add the HasChildren(ctx) function to the release, which will give an understanding.

So, i want to do like this one:

if children == nil {
	<input { *ic.Attributes... }/>
} else {
	{ children... }
}
@a-h
Copy link
Owner

a-h commented Oct 31, 2024

Good idea.

I propose a design like this:

func Children(ctx context.Context) (ok bool, c templ.Component) {
	_, v := getContext(ctx)
	if v.children == nil {
		return false, NopComponent
	}
	return true, *v.children
}  

So you could use it as:

if children, ok := templ.Children(ctx); ok {
  @children
} else {
  <div>No children</div>
}

This wouldn't replace the { children... } syntax, but { children...} is already redundant, since you can do @templ.GetChildren(ctx) to get the same result. It would be possible to update templ to replace { children...} with a @templ.GetChildren(ctx) statement if we wanted to slim down, but it's out of scope for this.

To implement this, we'd need some generator tests to ensure that if, if / else behaviour works OK, and that you get a NopComponent instead of nil if there are no children to prevent accidental panics where the ok value is ignored or used incorrectly. We'd also need some unit tests for the runtime function.

Happy to take a PR for this if someone else wants to implement, also happy to hear comments on the proposal, including naming the function something else.

@epicbytes
Copy link
Author

Acceptable, it looks like due to the early calculation of child components it would be logical to use a flag whether the child components were initially passed or not, because the order of the call would then matter.

@a-h
Copy link
Owner

a-h commented Nov 9, 2024

I'm not sure I completely follow. Are you suggesting a different API?

We have an experimental package at https://github.com/templ-go/x - that might be a good place for a proof-of-concept design.

@a-h a-h changed the title Feature request: HasChildren feature request: templ.HasChildren function Nov 9, 2024
@a-h a-h changed the title feature request: templ.HasChildren function feature: add templ.HasChildren function Nov 9, 2024
@a-h a-h added enhancement New feature or request NeedsInvestigation Issue needs some investigation before being fixed labels Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request NeedsInvestigation Issue needs some investigation before being fixed
Projects
None yet
Development

No branches or pull requests

2 participants