228 points · by nalgeon · 2y ago · 98 comments
This backlash is quite understandable. I believe Ember/React/Vue/Angular/etc. were all created with good intentions, but with the ecosystems of bolted-on technologies and now-necessary build tools, they have all become unmanageable monstrosities.
So people want to go “back to basics”. Personally, while I loved Ember with CoffeeScript (talking to a Django server via JSON) in 2015-2017, I can’t even revive the app I built without a massive effort to upgrade and ditch all the deprecated, unsupported dependencies (which might take more time than modifying my actual app code).
So I’m just going to rewrite the Web client with htmx (with Django rendering the actual templates). No churn and dependency hell, no build tools, no nonsense. Just a single script tag and some HTML attributes. For special cases that can’t be covered by htmx, there’s hyperscript (just add another script tag). Since browsers intend to maintain backward compatibility indefinitely, forced upgrades should be minimal to non-existent.
It’s a shame JS ended up being perceived as a villain, because it was meant to enable rich client applications within browsers, which are the most universally-compatible and commodified client you can possibly run such apps in.
And for the client-side stuff, you get to pick how to tackle it: plain javascript, htmx, web components, react, vue, svelte, qwik... all enjoy first class support.
For what it’s worth, I also think Django needs a better elevator pitch, but Django captured me in 2010. I might try pushing for a home page improvement. Django does have tenure and mindshare working in its favor, though.
Do folks know of standard libraries or patterns in Go or Rust to generate HTML without string templating? (As mentioned elsewhere, I'm looking for a backend for HTMX)
https://github.com/julvo/htmlgo
https://github.com/rohanthewiz/element
I'm sure there are many more.
The thing about these is that performance is often not as good as when using templates, especially when the templates are compiled to native code. Quicktemplate [1] is still the leader here IMO, and I don't think the OP project brings much that couldn't be done pretty easily with QT.
It also has an HTMX plug in.
Relevant libraries, for those who want to dive directly into it:
gomponents is amazing! I find the abstractions just right. it now looks possible that I can port my minimalistic bun/deno setup with htmx to go.
I found "gow" watcher that allows auto-restart of the development webserver when go code was changed. And I'm using "hx-refresh" header paramter and "hx-trigger='every 250ms'" to have a simple polling browser refresh.
it also integrates well with tailwind css. you can use tailwind vscode extension and define a custom "classRegex" for gomponents "Class(...)" function.
I needed to write a custom `Classes()` function that allows me to have conditional classes like `clsx()`. it's a bit hacky, but I like the simplicity.
func Classes(nodes ...g.Node) g.Node {
var classes []string
for _, node := range nodes {
if node != nil {
var builder strings.Builder
_ = node.Render(&builder)
c := builder.String()
c = strings.TrimLeft(c, " class=\"")
c = strings.TrimRight(c, "\"")
classes = append(classes, c)
}
}
return Class(strings.Join(classes, " "))
}
the runtime panics for functions that use vararg for optional arguments and have an invalid number of arguments is probably an okayish trade-off. mixing attributes and children elements in the builder functions, I don't know if it's good or bad, but I like the simplicity; JSX and hyperscript-like functions separate tag, attributes, and children into three different parameters.edit: reformat and typos.
but otherwise, I'm quite liking it so far
So I think it’s perfectly reasonable not to use tempting if it works for you. Especially if the templating isn’t very good, which is frankly most of them. I do think some templating can be good for prototyping, but as soon as you’re doing the same thing a lot of times and that thing doesn’t really change beyond what happens to the underlaying data structure, I think it’s very worth it to build it programmatically. Which is basically a lot of data CRUD UI in many organisations.
https://github.com/lambda-fairy/maud
It is refreshingly different from other Rust templating libraries. It uses a proc-macro that compiles your HTML into Rust code. I also happen to use it in conjunction with HTMX and it works very well for me (at least in small projects). You will still have to learn a little bit of "special" syntax though, which you are looking to avoid if I understand you correctly.
The templ language linked here is compiling something that looks similar to React's JSX into Go code. And then you can do HTMX: https://templ.guide/server-side-rendering/htmx/
The most compelling to me is that Templ templates are compile time safe, rather than crashing at run time like the built-in templates.
LAMP, MEAN, JAM and now GoTH...damn! web people sure have good taste in naming things!
The approach works surprisingly well, both server side and running in the browser (JSX, or Scalatags+Scala.js). There are plenty of websites that don't need heavy client-side interactivity where server side templating + a sprinkling of client side logic works just fine. This is especially true if you can share code between client and server (e.g. via JSX/React or Scalatags/Scala.js)
You get typechecking, interop with normal code, and a high performance runtime for free. That's a lot to like!
Code generation is something I saw in Flutter/Dart a lot and it entirely killed my interest in it (mostly with JSON serialization/deserialization to classes).
Thankfully, it's by no means a show-stopper for me thanks to their hot reloading functionality. https://templ.guide/commands-and-tools/hot-reload
'fd . 'assets/view' | entr templ generate' - couple it with 'air' and developing is a lot easier. Looking forward to when Goland has better support for templ.
https://pkg.go.dev/html/template
Which is based on top of text/template.
To very common things to do when templating is
- to include a sub-template (e.g. the article template includes the sidebar template)
- to specify that the template I work with and probably others should be rendered inside a slot in a containing template (e.g. the article template should be rendered into the general root template that contains the public menu but the admin pages should render in another root template)
Last I checked only the first of these use cases were supported.
I don't like Go's template engine too much, I find it a bit cumbersome in many cases. But it's quite capable.
there's an obvious typo of a variable name in the template "Titl" instead of "Title". Program compiles. Parse() succeeds. It just fails at runtime (outputting "<title>" and stopping. My understanding is that a Templ equivalent would probably end up with a compile error much earlier on (and probably more obvious in your IDE as well).
If this sounds to the reader like a defense of Go templates... it isn't.
Plus, I would agree that while it can do almost anything you want, that doesn't mean it always does it in a good way, or in a way that makes sense if you aren't a programmer. It's a pity. There's some power there, and I like the attempt at making templating safe, but it could really use a v2.
My problem with Hugo is how the templating becomes very confusing and the flow convoluted. The projects required places for various things becomes a maze, especially if you are not familiar with the site and theme you are working on.
I definitely prefer JSX to Hugo, but this project might be a nice middle ground.
If one wants to go Go+HTMX route, how does one write a webapp with handling auth and sessions and everything? I assume the backend is not just an API?
It'd be nice if one points to an example repo developed with this method.
But, I am curious why they decided to go with reserving keywords (like if and for) instead of doing what JSX does and just wrapping any Go code in braces.
Which makes it funny to then realize that e.g. twig is a templating language written in a templating language.
There is a very simple and productive subset of PHP that I feel got neglected over the last 1-2 decades.
The simple, but relatively fast “array”, which has value semantics.
The batteries included networking (HTTP, SMTP etc.) and SQL stuff.
The stateless execution model and straightforward way to combine and execute scripts.
And finally the in-built, seamless HTML templating.
It’s unfortunate that especially the last one got neglected so developers feel the need to build a safe abstraction over it.
It has some nice properties: streams by default, it’s pretty fast for a templating language and you get a lot of stuff for free that you eventually need.
However, you have to escape and sanitize output explicitly, which is not a thing in pretty much any modern templating language.
The std lib in go for example has a very explicit security model.
The problem is that you have to escape differently depending on the micro context. So it can become tedious and error prone if you have to do it explicitly all the time.
Idk if it has to with "modern" or not. Proper escaping contexts (whether, and which delimiters to quote in attributes, in content, in markup declarations, ...) are already defined by SGML, with HTML-aware templating or other vocabulary-specific expansion such as suppressing <script> elements injection as would be required by basic user commenting also being covered by SGML. My impression was that go/templating (and maybe pug?), while considered advanced relative to other templating packages shitting strings into markup, still requires explicit sanitation depending on the insertion context.
TAL -Template Attribute Language
https://en.m.wikipedia.org/wiki/Template_Attribute_Language
which happens to have an (also old) implementation i Go https://github.com/owlfish/tal
I wonder what opinion HN has on this type of templating language?
This seems to be much nicer.
Edit: Yep, codegen based.
Is there a way to do code colocation with templates? I mean for writing presentation logic alongside the html.
Asking because it seems templ functions only allow html.
The developer experience is ridiculously nice in that ecosystem if you’re accustomed to it. So many QOL things have been dealt with and polished.
But I do love the idea of something like that dev experience with Go instead of JavaScript powering it.
Maybe this would be totally sufficient if you don’t want any SPA/interactivity features?
Some sort of live pipe to component state like LiveView or similar could make it so a lot of JS would work implicitly, which might give a lot of the dev experience and user experience ease that people want. I’m not sure. But I do think something would often be missing with this library.
Regardless, I love the idea and I’d be stoked to see it grow and improve the developer experience.
If you're going to shoehorn everything into "this is an app" or "this is a document" then you're going to have problems no matter what you choose.
My JavaScript modifies the DOM rather than creating it.
With every project that grew the need for UI state I either wished I went with client side rendering or did so from the start for the parts that needed it.
The issue is: manipulating the DOM can become hard to reason about. You either somehow manage to use the DOM as your source of truth, or you manually sync your state into and out of the DOM, and other things like the URL etc.
The “immediate mode” style GUI model is much easier to reason about, because you can think of each render as a fresh start.
You don’t need React for this, in fact other libraries do this faster and without additional complexity such as Lit. Plus if you use something with web components, it is easier to mix and match client side and server side templating.
So (as usual) the key is to find the balance that works for you instead of pursuing one way (only server-side rendering or only SPA Javascript framework).
For the time being this approach works very well for me.
> My JavaScript modifies the DOM rather than creating it.