HTML framework for Swift

When I was doing some hobby project on swift server side. There is quite a few times I also need to write some frontend code.(Simple CSS + HTML or some advanced feature using vuejs and tailwindcss)

For most case our frontend data is from our swift server, so it will be great they can communicate with each other directly.

Currently there are already some great frameworks in the community:

And I have tried them all. But all of them have some sort of limitation for me.

Therefore, I come up with the following questions:

  1. Does frontend is included in server side or is it a concern from Swift Server Workgroup (SSWG)?
  2. If question 1 is Yes. Is there any plan for SSWG to make some kind of "official" HTML framework?

For my server app, I first use vapor/leaf and then transition to use HTMLKit. And recently I transitioned to use Plot.

The following are just my personal subjective comments, which have nothing to do with this question. All the mentioned HTML frameworks are great frameworks and I have great respect for the maintainers of these libraries.

My feelings

Leaf is a great and comprehensive framework, but it is a little too complicated to config and use.

Both HTMLKit and Plot aim to provide some sort of SwiftUI-like DSL for HTML.

HTMLKit is simple and elegant and the TemplateValue is a great effient tool to use. But sometimes TemplateValue could also be a trouble to deal with.

Plot is currently my most satisfied framework for my server app. (Not saying the former 2 is bad, it just fits my need best currently.) But it still has some potiential problem.

For example, the Component protocol is used everywhere as a extensial type.

// SwiftUI.View
public protocol View {
    associatedtype Body : View
    @ViewBuilder var body: Self.Body { get }
}

// Plot.Component
public protocol Component {
    var body: Component { get }
}

I think we need to clarify the SSWG's role a bit here, the workgroup members may sometimes indeed step up and write up some library, but in general it should not be the expectation that the SSWG members write all meaningful libraries -- that would not scale, and we need to scale in order to make Swift great everywhere! :slight_smile:

As of today's plans and focus areas: we are not actively looking into templating languages. The SSWG could absolutely recommend (any number of) mature templating libraries - note that it is never the goal to just "choose one", we just want to signal to people that "yes, this library is well maintained" with the SSWG Incubation process.

Having that said... certainly more exploration and/or more libraries or improving the existing ones is definitely a goal worth pursuing by the community :slight_smile: We can help where we can... but I don't think any active work on these is likely to happen from the SSWG members, at least not right now...

To put this in a more productive tone then: perhaps the feedbacks you have could be converted into some actionable points or tickets that can be communicated to those framework authors? WDYT?

Paging @0xTim since Vapor Leaf and Vapor Communit HTMLKit were mentioned

9 Likes

I've tried to communicate with some of the maintainers. And I think leaf and HTMLKit's "problem" for me is by its design and will not change a lot unless a new major release.

For me, Plot looks most promising and feels most comfortable for me(The usage gramer and learning curve). But unforunately, it seems not actively maintained as the other 2 frameworks mentioned above.

Got it, thanks for your elaborated explanation.

I can definitely see LeafKit getting pitched to the SSWG once we've done some of the others (like JWTKit). The set up can be a bit involved if not using Vapor but I've found it really useful when working with front-end devs/designers as they can still write mostly HTML and create great looking sites/emails and only need to learn some basic syntax instead of all of Swift.

In terms of SSWG priorities for front-end stuff I'd say personally it's not a huge priority atm. The front-end world is heavily invested in SPAs and until WASM gets much better support that's unlikely to change

4 Likes

i use swift-dom to build HTML. i too detest HTML builders in general, and it took me five breaking versions for me to get swift-dom to a point where i was semi-satisfied with it. to be honest, i am still not completely happy with it, but at least i can use it now without constantly feeling like i was using a clunky API.

if you are interested, the docs for swift-dom are here: Swift DOM Documentation

2 Likes

I use my SwiftXML library to output HTML. The library is generally for XML and easy transformation of it, but has a "production" for HTML to output HTML as it should.

1 Like

swift-html is also a great 3rd-party library to build HTML code using Result Builder.

1 Like

Funny I should come across this thread. I'm kind of in the same boat. I've had this idea for a web app for a long time and recently decided to get serious about it and try to build it. I started out using Python and frameworks like Django, Flask and FastAPI and really enjoyed the experience, but wanted to become more proficient at Swift as well, so switched to Vapor.

I started out with Swift-Html which is really nice, and the author has a good book about using it, but ultimately, there while it was a great experience, I just felt the need to deal with HTML at times. I really just wanted something similar to Django/Jinja2 templating.

Then I tried Stencil. I REALLY liked it and figured out how to extend it to a level of functionality close to what exists in Python. I even tried to get it to integrate it with Vapor to make it feel as native as Leaf, but unfortunately it was beyond my current knowledge.

I finally tried Leaf, which I guess should have been my starting point. It's actually not too bad. The syntax is a little ugly, but it's not too far from what I had. I like how easy it is to extend tags, but it is otherwise a little bare with what ships compared to what you would typically get in Python frameworks. I also wish it was a little more obvious on how to extend it to provide things like rendered blocks for instance, but that might just be my lack of Swift expertise. I do think with a little bit more love Leaf could be the go-to templating language for Swift.