With some caveats^, Swift's result builders are probably the nicest templating system I've used. It's expressive enough kinda look like what you're moddeling.

For example with the HTML DSL ElementaryUI:
ul {
for item in listOfSomething {
li { item.string }
}
}

Yet you're still fully in the programming language you're in.

Result builders have pretty much convinced me that any language that might at any point be used to generate UIs should seriously consider having some kind of DSL feature like this.

Having it fully integrated into the language in a natural way makes generating UIs and similar things so much more natural to deal with.

And don't talk to me about these dedicated templating languages. In those you typically loose strong typing (among other things) completely, forcing you to find mistakes at run time instead of compile time.

@torb I totally agree ; )

@elementarycodes Thanks for making Elementary!

I looked at a couple of HTML DSLs for Swift and it worked the best by far. (I will admit that I've fought the generic inferene of some HTML… but I suppose that's the price of helping the Swift compiler to optimize things).

I have a question though: what is the idiomatic way to optionally add an attribute? Currently, if there's only one option I simply have two cases:

case let .link(text, .some(uri), .some(title)):
a(.href(uri), .title(title)) { text }
case let .link(text, .some(uri), .none):
a(.href(uri)) { text }

I'm thinking it would make more sense to make them the same case and then have it optionally included in some way.

For when I have more than a single attribute that is optionally included then having several cases is not very practical as the possible combinations end up way to many cases.

elementary Documentation – Swift Package Index

@elementarycodes I was kinda using the first (and I suppose a variant of that is what I'll end up with).

Your second option does express intent quite well, but the compiler cannot prove that my optional value exists so it's in principle unsafe since I have to assert it's there with bang!:

case let .link(text, .some(uri), title):
a(.href(uri)) { text }
.attributes(.title(title!), when: title != nil)

I can imagine this (having an optional value that you then want to put into a attribute if it exists) to be something that could commonly happen.

That said, I'm stil very new to actually programming in Swift myself, so there might be a far more elegant way to do this in the language that I am not aware of.

EDIT: I suppose this second thing isn't going to work anyway considering that it's eagerly evaluated (but it was a messy solution anyway).