Live data from Hacker News

Show HN: Use Go's HTML/template to write React-like code

news.ycombinator.com

51–60 of 96 posts

Re: Show HN: Use Go's HTML/template to write React-like code

#51

OMG! No! Having dealt with Go templates in the context of writing Helm charts, I would say this is not a a great idea. Please, invent a better templating language for Go, and then perhaps go down this path. That being said, I am not a great fan of templates in any language. Jinja2 might be a heck of a lot better than Go templates but it can still be hard to get right.

What is so wrong with this approach that makes you exclaim "OMG! No!"? This is practically no different to any basic server side rendering framework in any other language. This is a very basic pattern used all the time.

Re: Show HN: Use Go's HTML/template to write React-like code

#52

I like the experimentation, but it's still using templates. I was hoping something pure-Go code components without any template rendering at all. I was trying to do the same React-like Component framework, but for Python. No templates needed, you write 100% Python code. Here are some examples how it looks like for simple components: https://github.com/kissgyorgy/compone/tree/master/core/examp... I use it in a Django…

We have been doing something similar with https://htpy.dev the last ~6 months.

Re: Show HN: Use Go's HTML/template to write React-like code

#53
I've been using Go's html/template for the last couple of months and all I can say is that it's still very much a toy. It's fine for basic loops and conditionals but anything beyond that is extremely limited and having proper reusable components is a constant fight against limitations, specifically because of the lack of being able to pass multiple arguments to a template. I wish the Go team had gone with something more akin to PugJS[1], which in contrast is very rich in features, flexible and dare I say fun to build components and pages with.

[1] https://pugjs.org/api/getting-started.html

Re: Show HN: Use Go's HTML/template to write React-like code

#54

I've been doing something very similar in Rust recently using Askama or Maud for templates (components), optionally with Axum to include a server in the binary. The author mentions wanting to colocate templates with logic, Maud allows much tighter/automatic integration in this regard. This approach also synergizes almost perfectly with HTMX.

I do this but using hypertext, since the syntax is closer to JSX and less confusing for others to touch.

Re: Show HN: Use Go's HTML/template to write React-like code

#56
post #45

I've been doing something very similar in Rust recently using Askama or Maud for templates (components), optionally with Axum to include a server in the binary. The author mentions wanting to colocate templates with logic, Maud allows much tighter/automatic integration in this regard. This approach also synergizes almost perfectly with HTMX.

> Maud allows much tighter/automatic integration in this regard Interesting, can you share some examples?

In the linked Go approach, and also with Askama, you have to define each template (component) twice. Once as a .html file, and then again as a struct inside a source file.

With Maud, templates/components are only defined once, so you no longer have to worry about keeping the two in sync or colocating them.

This is because Maud provides a macro for writing HTML with Rust-like syntax, so all your HTML (components) ends up inside regular Rust files, and you can refer to variables as usual.

This really makes for almost seamless integration between Rust code and the HTML you serve.

Re: Show HN: Use Go's HTML/template to write React-like code

#57
post #14
post #4

[off-topic comment but somewhat relevant for anyone choosing a lang/ecosystem] I noticed that the syntax highlighting for the Go html/template examples in the article is incomplete. We're using Go html/templates on a new project (within a team that does a lot of React frontend & some Python) so this kind of project seems like it would really suit, but a massive bugbear has been the very poor IDE-level syntax highligh…

In Jet Brains family IDEs (not sure how they call the go one) you can use Inject language or reference option to get most of editor support (inspections,highlighting,intellisense etc.) for snippets of one language embedd in files of another language. I've been using it with JS scripts inside custom xml for a while and it works pretty well.

JetBrains "Inject" is fine for contiguous embeds like JS in XML, but doesn't cut it for inline intertwined string-templating.

E.g. (from the article):

  

Re: Show HN: Use Go's HTML/template to write React-like code

#58

Interesting. I am actually doubling down on building web apps in Go using mostly standard library, powerful templates and sprinkle in with a library such as Alpine JS. Seeing some success so far and it makes me so much happier knowing that I don't have to deal with node_modules BS (well may be except tailwind for css). I actually want to test the limits of how far one can go without using a JS framework like React/Vu…

This was exactly my journey too. And I went from a server side templating to going deep into Nextjs/react and then back to htmx/go templates. Every time I think "oh this pattern would have been possible with react" I find a much simpler way. All that dependency bs and bloat was just not worth it.

Re: Show HN: Use Go's HTML/template to write React-like code

#59
post #54

I've been doing something very similar in Rust recently using Askama or Maud for templates (components), optionally with Axum to include a server in the binary. The author mentions wanting to colocate templates with logic, Maud allows much tighter/automatic integration in this regard. This approach also synergizes almost perfectly with HTMX.

I do this but using hypertext, since the syntax is closer to JSX and less confusing for others to touch.

Just checked out hypertext! Have you noticed any other advantages over Maud, except for syntax?

Re: Show HN: Use Go's HTML/template to write React-like code

#60
post #24

My style for building web app. I was never convinced that doing everything on JS layer is a good thing (regardless the backend language). But you need to show more interactivity to convince the JS people. I personally like this style and add HTMX for partial updates.

but JS is the only answer to client side interactivity without hacks, there's nothing to be convinced
Post reply on HN