News for hackers. Stuff that matters.
Re:Maud: A Rust macro for writing HTML by Grimburger on Tuesday June 07, 2022 @03:31AM (#31650949)
Maud is wonderful for small things like a stats page or dashboard.

But once you start getting to a few pages it's so much less cognitive overhead to separate concerns out into proper html files/templating.

It seems quite daunting to have a large project where all your html is inside rust files and a lot harder to find people who can work on it proficiently.

Re:Maud: A Rust macro for writing HTML by rtpg on Tuesday June 07, 2022 @08:48AM (#31653427)
I do, in theory, understand separation of concerns.

But as someone who works on a sprawling set of template files with loads of sprinkled conditionals in a template language... the idea of using a "real language" for the templating, and in particular having type checking and proper abstraction tools (you can just write functions! No weird template tag tricks) is so much nicer to me.

HTML templates have always felt super brittle to me, basically at the level of C preprocessor programming. Even just with React/TS you can get away with so much with way more confidence given the typed backing.

Though I think that you _could_ get away with some nice tricks in Rust thanks to macros, where you ingest the template files and generate render functions with all the properties... or something like that.

Re:Maud: A Rust macro for writing HTML by zaarn on Tuesday June 07, 2022 @04:28AM (#31651338)
One of the huge advantages of Maud is that it keeps code and markup close together, so you can do things like having rust control structures determine what markup is used later on (for snippets or widgets, for example).

Plus, Maud has a definitive speed advantage from what I've noticed.

Sadly, there isn't any great alternative for making performant HTML templates in Rust from what I've seen, a lot of the other HTML templating libs aren't that great comparatively.

Re:Maud: A Rust macro for writing HTML by rbanffy on Tuesday June 07, 2022 @05:34AM (#31651814)
> One of the huge advantages of Maud is that it keeps code and markup close together

I'd say it's actually a disadvantage. Front end engineers will work with HTML with embedded logic (as little as possible - values, iterators, conditionals). Generating HTML from Rust code is nice for simple things, but won't work with complicated HTML templates when those are required and will be harder to integrate with front-end applications. It's possible to compile a template with embedded logic (Java Server Pages did that in the late 1990's) to performant and type safe code. An approach like that would be great for Rust (someone must have done it by now)

Re:Maud: A Rust macro for writing HTML by zaarn on Tuesday June 07, 2022 @05:39AM (#31651837)
My projects have no front-end engineer, however. The frontend is written in Rust as well and also uses Maud for HTML templating.

And I dispute it not working with complicated HTML templates, that's handling it fine and integrates very well since I share code between frontend and backend anyway.

Re:Maud: A Rust macro for writing HTML by rbanffy on Tuesday June 07, 2022 @05:43AM (#31651875)
I get that, but Maud is not the first of its kind and the absence of similar engines in web development is a telltale sign it's not the optimal approach.
Re:Maud: A Rust macro for writing HTML by zaarn on Tuesday June 07, 2022 @05:47AM (#31651890)
Well, as mentioned, I've yet to find something that can replace Maud without sacrificing it's advantages (in-code inheritance, code-sharing and compile-time type-safety).

So even if it's not the "optimal approach" as you title it, it's the ergonomic approach for me because it doesn't get in my way.

Re:Maud: A Rust macro for writing HTML by rbanffy on Tuesday June 07, 2022 @07:11AM (#31652450)
For these requirements, it's indeed a good choice. It's just not a use case I've seen often.
Re:Maud: A Rust macro for writing HTML by mkj on Tuesday June 07, 2022 @06:10AM (#31652045)
Askama should be comparable in speed and type safety? It probably doesn't integrate any better inline with source though (I've only used it with standalone template files).

https://djc.github.io/askama/creating_templates.html#the-tem...

Regardless, it's good to see some variety and experimentation like Maud in software design

Re:Maud: A Rust macro for writing HTML by zaarn on Tuesday June 07, 2022 @07:12AM (#31652456)
Askama looks interesting, though the inheritance is a bit underdocumented.

Ideally, I'd imagine just making a "Global<T>" struct, where I insert the current page context like so "Global<LoginPage>" to get the final template. Though by the looks, it sounds more like LoginPage will simply have to have all the global context available via some attribute like "LoginPage.global".

Re:Maud: A Rust macro for writing HTML by dochtman on Tuesday June 07, 2022 @05:03AM (#31651615)
> Sadly, there isn't any great alternative for making performant HTML templates in Rust from what I've seen, a lot of the other HTML templating libs aren't that great comparatively.

Which others have you tried, and why do you think they're not great?

Re:Maud: A Rust macro for writing HTML by zaarn on Tuesday June 07, 2022 @05:10AM (#31651660)
I've tried handlebars and liquid. They're okayish, but both slow and don't have any compiletime help or typing when I tried them.

Horroshow, ructe, typed-html and yarte were okay but inheritance was underdocumented (or generally not as well documented), which is a big no for me. And a few only give minor-quality help at compile time.

Maud on the other hand has decent compiletime help and doesn't require a lot of documentation as normal rust code is doing the heavy lifting.

Re:Maud: A Rust macro for writing HTML by dochtman on Wednesday June 08, 2022 @04:55PM (#31673982)
I created Askama (which yarte forked), the inheritance docs aren't great but also not too bad. Curious why you didn't try it?
Re:Maud: A Rust macro for writing HTML by zaarn on Thursday June 09, 2022 @11:03AM (#31682092)
I haven't found askama yet, or rather, discovered it through HN here, I don't check the arewewebyet.org site too often and it's my main source of frameworks to find.

My main worry on things is that I'd rather like being able to express the inheritance in the type system through generics. Ie, having a top level "Page<T>" struct in which I can start putting the various subparts needed to render a page. It seems askama is doing it the other way round with a "parent" member needed in the child page.

Re:Maud: A Rust macro for writing HTML by Inviz on Tuesday June 07, 2022 @04:21AM (#31651284)
I wish it was still the consensus among JS devs. People are putting css into JS files, on top of html
Re:Maud: A Rust macro for writing HTML by vonadz on Tuesday June 07, 2022 @04:45AM (#31651472)
I think it isn't the consensus anymore because there isn't actually a "correct" solution. It's too subject to personal preference. For example, I prefer having HTML + CSS + JS all in the same file because then I know everything relevant to the component or page I'm working on is right there. I do concede that it can get extremely unwieldy if you don't separate out components though.
Re:Maud: A Rust macro for writing HTML by My71staccount on Tuesday June 07, 2022 @08:57AM (#31653552)
There is a correct solution (the one you're not doing)
Re:Maud: A Rust macro for writing HTML by vonadz on Tuesday June 07, 2022 @10:46AM (#31655125)
Sure, just like there's a correct IDE.