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

docs: Inline method components #561

Closed
Ra0k opened this issue Feb 26, 2024 · 4 comments
Closed

docs: Inline method components #561

Ra0k opened this issue Feb 26, 2024 · 4 comments
Labels
documentation Improvements or additions to documentation NeedsFix Needs implementing

Comments

@Ra0k
Copy link
Contributor

Ra0k commented Feb 26, 2024

The language supports method components like this:

package main

type Data struct {
	message string
}

templ (d Data) Method() {
	<div>{ d.message }</div>
}

func main() {
	d := Data{
		message: "You can implement methods on a type.",
	}
	d.Method().Render(context.Background(), os.Stdout)
}

Would it be possible to add support for this kind of syntax?
Sorry, if it is already supported, I did not find anything about the specific syntax in the documentation nor a working example.

templ Hello() {
	@Data{
		message: "You can implement methods on a type.",
	}.Method()
}

This syntax could be a cleaner way of configuring renderable components than passing config arguments to functions.

@joerdav
Copy link
Collaborator

joerdav commented Feb 27, 2024

Hi there thank you for the report.

In-fact this syntax is already supported. Perhaps if you have time you might like to add an example to the docs please?

https://templ.guide/core-concepts/components/#method-components

@joerdav joerdav changed the title Inline method components docs: Inline method components Feb 27, 2024
@joerdav joerdav added documentation Improvements or additions to documentation NeedsFix Needs implementing labels Feb 27, 2024
@Ra0k
Copy link
Contributor Author

Ra0k commented Feb 27, 2024

@joerdav this syntax does not work for me as it is.

Extending the demo code with the following:

templ Hello() {
	@Data{
		message: "You can implement methods on a type.",
	}.Method()
}

will result in this error message:
Data.Render undefined (type Data has no field or method Render

@joerdav
Copy link
Collaborator

joerdav commented Feb 27, 2024

@Ra0k What version are you running of templ? Try installing the mainline version of the cli, the parsing has been improved: go install github.com/a-h/templ/cmd/templ@main.

@Ra0k
Copy link
Contributor Author

Ra0k commented Feb 27, 2024

Thank you, @joerdav! I haven't realized that it's now supported in the new version! I can confirm that it indeed works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation NeedsFix Needs implementing
Projects
None yet
Development

No branches or pull requests

2 participants