-
Hi all,
I think this is very useful when treating templates programmatically... Thanks for suggestions! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
a little late to the party but I've just tried this: templ Index() {
<h1 class="text-4xl font-semibold">Index</h1>
<div>
<h2 class="text-3xl mb-4">Buttons</h2>
</div>
if true {
{{ return }}
}
<dialog id="the-dialog" class="rounded"></dialog>
} And as would be hoped the I'm still not sure that this would be something I'd expect to see in a real code base personally, since I'm not sure what side-effects there may be to returning early. I can only imagine tag endings being missing but there's probably more side-effects that I'm not imagining. This relies on the Raw Go experimental functionality. |
Beta Was this translation helpful? Give feedback.
-
I think of templ components as being like having an implicit yield of each node, so they don't fit into a very procedural style. // Psuedo-code.
templ Component() {
yield(<div>)
yield("some text")
yield("</div>")
} I'd probably do what you're thinking about in Go code. func ErrorOrComponent(err error, c templ.Component) templ.Component {
if err != nil {
return DangerAlert()
}
return c
} Then I could use it wherever I needed it: templ MyComponent() {
<div>Hello, World!</div>
}
templ IndexPage(err error) {
@ErrorOrComponent(err, MyComponent())
} |
Beta Was this translation helpful? Give feedback.
-
Thank you all for input. RawGo is what I'm looking for. I tried it and it compiles as advertised. A bummer, because I really want to to use plain go in the templates. Here is my vote to continue raw go development and make it a stable feature. |
Beta Was this translation helpful? Give feedback.
-
It's been promoted to stable in 88eed0f, we just haven't cut a release yet. |
Beta Was this translation helpful? Give feedback.
a little late to the party but I've just tried this:
And as would be hoped the
dialog
is not rendered.I'm still not sure that this would be something I'd expect to see in a real code base personally, since I'm not sure what side-effects there may be to returning early. I can only imagine tag endings being missing but there's probably more side-effects that I'm not imagining.
This relies on the Raw Go experimental functionality.