Live data from Hacker News

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

news.ycombinator.com

41–50 of 96 posts

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

#41
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 project everywhere, no template rendering for custom views!

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

#42
post #36
post #27

A bit off-topic, and maybe it was just an example made without too much thought put into it, but I've noticed there seems to be a lot of people now who write button/links like this: My Button As far as I know that's technically invalid HTML. You can't have an as a child of a (or even the other way around.) I'm also ignoring the invalid type="submit" against the tag in the example button component in the post. I think…

Once the major browser engines support it, it's de-facto legal. This also explains why there are so few alternative browsers. Writing a new browser that supports everything that happens to work in the major browsers is ridiculously hard.

Yes, html is one of the unique languages of our era. It's very forgiving and lenient.

> This also explains why there are so few alternative browsers. Writing a new browser that supports everything that happens to work in the major browsers is ridiculously hard.

IMO one of the reasons Chrome won was because they supported both Firefox and IE's interpretation of web standards.

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

#43
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/Vue especially for smaller teams.

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

#44
post #25

https://github.com/a-h/templ is a similar mature project that has JSX style templates but using Go instead of Javascript with good VS code support etc.

Except that its syntax choices are a little odd, instead of making all code inside curly braces, there are some special keywords which only mean something if they are at the beginning of a paragraph (iirc).

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

#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?

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

#46
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.

It depends strongly on what you’re building. Interactivity is a property of the app and different types of apps have different interactivity requirements. For example a page that is more or less a UI proxy for CRUD operations typically does not require too much client side state. On the otherhand, a flow chart editor (any document editor really) needs to function largely without round trips to the server. If the web app is intended for mobile, you would expect to see many more interactions with limited bandwidth or no internet connectivity, so you would need to account for that. This is much more about application architecture than preference (although you can certainly choose to only work on applications that match your preferences).

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

#47
post #6
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…

> I noticed that the syntax highlighting for the Go html/template examples in the article is incomplete. Sorry, it might be something wrong with my blog setup. I use hexo, might need to play around with the config. I use this VSCode extension for coding and it works fairly well: https://marketplace.visualstudio.com/items?itemName=jinlimin...

This is something that should be part of gopls IMO.

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

#48
This is a nice approach I've followed myself before. It feels like common sense web development, just writing things in a way which allows them to be reused. A button will likely be the same on any page of the site, so make a button component.

I don't think this is "React-like" though. Making components isn't specific to React. We've been doing that since ASP and probably earlier. Your views still need to know what components to render here.

You can go further and just define a template function which takes a template name and a data object. Then render arbitrary children. That way your UI can be fully composed in code and the only thing your components need to know is how to render themselves.

    var templateFS = os.DirFS("./templates/components/")
    
    type Component interface {
     Type() string
    }
    
    type Button struct {
     Name string
    }
    
    func (b Button) Type() string {
     return "button.html"
    }
    
    type Div struct {
     Children []Component
    }
    
    func (d Div) Type() string {
     return "div.html"
    }
    
    type Page struct {
     Children []Component
    }
    
    func (p Page) Type() string {
     return "page.html"
    }
    
    func main() {
     component := Page{
      Children: []Component{
       Div{
        Children: []Component{
         Button{Name: "Test button"},
        },
       },
      },
     }
    
    templates := template.New("")
    funcs := template.FuncMap{
     "exec": func(tpl string, data interface{}) template.HTML {
      var sb strings.Builder
      templates.ExecuteTemplate(&sb, tpl, data)
      return template.HTML(sb.String())
     },
    }

    templates = template.Must(templates.Funcs(funcs).ParseFS(templateFS, "*"))
    templates.ExecuteTemplate(os.Stdout, component.Type(), component)
Your page.html file becomes

    
    
    
        
        Title
    
    
    {{range .Children}}
        {{exec .Type .}}
    {{end}}
    
    
For example

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

#49
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.

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

#50
post #18
post #9

Earlier quoted context omitted.

I love React too, but it pulls in a lot of dependencies and the dependencies need periodic maintenance. Sometimes you get the feeling that you're spending a significant chunk of your time not writing code but fighting with configs and dependency churn. Using Go's html/template definitely makes the UI feel dated because of re-rendering, but I rarely notice it in my side project. My next exploration is to use HTMX. I'm…

HTMX + React style server rendered components would solve 80% of the use cases for a modern SPA. Keep going! This is a breath of fresh air for many simpler applications.

I haven't tried HTMX, can it fetch and load html fragments into an existing dom?
Post reply on HN