# Replacing Leaf with a Swift DSL

**URL:** <https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585>\
**Category:** Vapor\
**Created:** [June 11, 2019, 8:58am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585 "2019-06-11T08:58:52Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeehut](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeehut/32/21743_2.png) [@Jeehut](https://forums.swift.org/u/Jeehut)\
**Post date:** [June 11, 2019, 8:58am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/1 "2019-06-11T08:58:52Z")

</div>

When Apple revealed SwiftUI last week during WWDC 2019, we also saw a new feature in Swift proposed as a draft on Swift Evolution ([Function builders](https://forums.swift.org/t/function-builders/25167)). Together with the [Property wrappers](https://forums.swift.org/t/pitch-3-property-wrappers-formerly-known-as-property-delegates/24961) proposal and some other recent Swift features, in Swift 5.1 it will be now possible **to define DSLs** that will enable code like this (screenshot from [this WWDC session](https://developer.apple.com/videos/play/wwdc2019/402/)):

 ![23](https://global.discourse-cdn.com/swift/original/2X/4/48962ce9b44255d92c7746708a614842f43da161.png)  
_Yes, this **is** Swift code!_

As you can see, it will soon be possible to natively support HTML by implementing a DSL for it in Swift (like [Vaux](https://github.com/dokun1/Vaux) does) with all the advantages that come along with it: Great compiler errors as feedback, autocompletion, more readable and flexible functions, Swift-native control flow etc.

While Leaf is a good template engine, I think it would make sense to introduce an alternative to Leaf based on such a DSL for HTML, CSS and the likes of it. What do you guys think?

---

<div class="post-metadata">

**Author:** ![svanimpe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/svanimpe/32/38963_2.png) [@svanimpe](https://forums.swift.org/u/svanimpe)\
**Post date:** [June 11, 2019, 10:03am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/2 "2019-06-11T10:03:58Z")

</div>

As a heavy user of templating languages, I'd be all for this. However, I do hope this will become a SSWG effort, so we don't end up with multiple incompatible DSLs.

FYI: There's also [GitHub - pointfreeco/swift-html: 🗺 A Swift DSL for type-safe, extensible, and transformable HTML documents.](https://github.com/pointfreeco/swift-html). I believe pointfree uses this for their website.

---

<div class="post-metadata">

**Author:** ![Jeehut](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeehut/32/21743_2.png) [@Jeehut](https://forums.swift.org/u/Jeehut)\
**Post date:** [June 11, 2019, 10:09am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/3 "2019-06-11T10:09:30Z")

</div>

I know of `swift-html`, but it doesn't use the new features in Swift (cause they weren't announced at that time) and the dot syntax they're using is much less powerful then what will be possible with Swift 5.1 onwards. Note that with the new DSL you won't even need commas between statements!

---

<div class="post-metadata">

**Author:** ![tanner0101](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tanner0101/32/675_2.png) [@tanner0101](https://forums.swift.org/u/tanner0101)\
**Post date:** [June 11, 2019, 3:46pm UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/4 "2019-06-11T15:46:31Z")

</div>

I think [GitHub - vapor-community/HTMLKit: Create and render HTML templates with HTMLKit](https://github.com/vapor-community/htmlkit) would be a great place to implement function builder support.

> [@Jeehut](#):
>
> While Leaf is a good template engine, I think it would make sense to introduce an alternative to Leaf based on such a DSL for HTML, CSS and the likes of it. What do you guys think?

Depending on how the `HTMLKit` package is received, the core team would consider making it an official package (moving it to vapor/\*) and offering it during `vapor new` project initialization.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 12, 2019, 4:32am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/5 "2019-06-12T04:32:10Z")

</div>

👋 Hi! One of the `swift-html` developers here.

Point-Free's `swift-html` library offers a DSL vision from before the era of "function builders". Because of this it doesn't (and couldn't) take advantage of the new, implicit syntax that function builders offers, but it's definitely not "less" powerful! If anything it's "differently" (and sometimes _more_) powerful. We want to explore what the Swift DSL syntax has to offer, and we plan on updating our libraries accordingly, but please understand that function builders introduce limited sugar on top of existing means of building DSLs in Swift.

The `swift-html` library lets you build HTML as first-class Swift values, which means arrays of non-HTML values can be mapped to arrays of `li` elements using the `map` that comes with `Sequence`. Function builders can't take advantage of these operations and instead requires custom `ForEach` operators to be defined per DSL.

In the end, function builders are an _early_ work in progress, and even though they read very nicely, they are much less composable right now. A few folks have pointed this out in evolution, so if this kind of flexibility is important to you, now's a good time to weigh in!

---

<div class="post-metadata">

**Author:** ![linqingmo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/linqingmo/32/2442_2.png) [@linqingmo](https://forums.swift.org/u/linqingmo)\
**Post date:** [July 6, 2019, 3:16pm UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/6 "2019-07-06T15:16:42Z")

</div>

Something like [SwiftWebUI](https://github.com/swiftwebui/SwiftWebUI.git)?

---

<div class="post-metadata">

**Author:** ![MajorTom](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@MajorTom](https://forums.swift.org/u/MajorTom)\
**Post date:** [December 2, 2019, 1:37am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/7 "2019-12-02T01:37:34Z")

</div>

I really like the idea of moving to a DSL. I have wondered how one would integrate some basic Javascript functionality for things like disabling buttons when all the required fields are not full, or client-side validation. There are many cases were a bit of interactivity would be all that was needed, not a full single page application.

---

<div class="post-metadata">

**Author:** ![tonyarnold](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tonyarnold/32/30924_2.png) [@tonyarnold](https://forums.swift.org/u/tonyarnold)\
**Post date:** [December 2, 2019, 4:04am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/8 "2019-12-02T04:04:13Z")

</div>

@MajorTom have you tried using something like `HTMLKit`? That's a fairly straight-forward thing to support right now.

Leaf falls apart if you try to do anything with in-page HTML script blocks.

---

<div class="post-metadata">

**Author:** ![MajorTom](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@MajorTom](https://forums.swift.org/u/MajorTom)\
**Post date:** [December 2, 2019, 6:22am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/9 "2019-12-02T06:22:06Z")

</div>

I looked briefly at both HTMLKit and Swift-HTML (with the Vapor version), and was not really clear how I would add the JavaScript. It is probably easy, but I did not see how I would do it. 🙂

---

<div class="post-metadata">

**Author:** ![tonyarnold](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tonyarnold/32/30924_2.png) [@tonyarnold](https://forums.swift.org/u/tonyarnold)\
**Post date:** [December 2, 2019, 6:33am UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/10 "2019-12-02T06:33:05Z")

</div>

I can't check right now but as far as I know, HTMLKit has a Builder that would allow you to do something like:

```
Script {
"""
// Write your JS here.
"""
}

```

---

<div class="post-metadata">

**Author:** ![MajorTom](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@MajorTom](https://forums.swift.org/u/MajorTom)\
**Post date:** [December 2, 2019, 4:02pm UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/11 "2019-12-02T16:02:57Z")

</div>

I did not find script as one of the tags they generate, but I may have missed it.

---

<div class="post-metadata">

**Author:** ![kylebrowning](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kylebrowning/32/9837_2.png) [@kylebrowning](https://forums.swift.org/u/kylebrowning)\
**Post date:** [December 10, 2019, 5:49pm UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/12 "2019-12-10T17:49:53Z")

</div>

FWIW this now exists too [GitHub - JohnSundell/Plot: A DSL for writing type-safe HTML, XML and RSS in Swift.](https://github.com/JohnSundell/Plot)

---

<div class="post-metadata">

**Author:** ![LemonCupcake](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@LemonCupcake](https://forums.swift.org/u/LemonCupcake)\
**Post date:** [June 9, 2020, 12:07pm UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/13 "2020-06-09T12:07:22Z")

</div>

I don’t think Sundell’s repo uses the DSL @Builder or @functionBuilder though.

---

<div class="post-metadata">

**Author:** ![kdeda](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kdeda/32/14938_2.png) [@kdeda](https://forums.swift.org/u/kdeda)\
**Post date:** [March 16, 2023, 2:06pm UTC](https://forums.swift.org/t/replacing-leaf-with-a-swift-dsl/25585/14 "2023-03-16T14:06:34Z")

</div>

A bit late to the party, but wanted to point out that out of all of the options mentioned here, [SwiftWebUI](https://github.com/swiftwebui/SwiftWebUI) seems to be using a proper DSL (fairly well implemented imho) to render SwiftUI looking code into HTML/CSS, but most importantly it will also bind back actions.  
The following is what you write, ie: the SwiftUI looking code.

```swift
// Note that The View here is a SwiftWebUI.View rather than a SwiftUI.View
// but this makes reading this code fairly trivial
struct OrderPage: View {
    @State var order = Order()

    var body: some View {
        Form {
            Section {
                Stepper(value: $order.quantity, in: 1...10) {
                    Text("Quantity: \(order.quantity)")
                }
            }
        }.padding()
    }
}

```

Their example of the [AvocadoToast](https://github.com/SwiftWebUI/AvocadoToast) does a fantastic job of building a proper web application.  
Which none of the other choices mentioned come close.
