PurpleKingdomGames / tyrian

Elm-inspired Scala UI library.
https://tyrian.indigoengine.io/
MIT License
346 stars 26 forks source link

Rethinking Html Rendering, a la SSR #235

Closed davesmith00000 closed 8 months ago

davesmith00000 commented 8 months ago

This is in response to issue #231, which calls for a nicer way to declare the doctype.

The old way of rendering HTML directly, which is to say not via the virtual dom, for the purposes of SSR or another JVM html use case, was to do something like this:

val includeDocType = true
val model = Int
val view = (i: Int) => html(p("count was " + i))

TyrianSSR.render(includeDocType, model, view)

Mostly ok, kinda, except that includeDocType is just a boolean, and I was quite taken by ScalaTags approach of just letting you prefix a string so that the doctype could be whatever you wanted.

So I took out includeDocType, and while wondering what do do next, I organised the render extensions methods, then I realised that asking someone to supply the view and the model, just so that I could call view(model).render for them in a clunky way via TyrianSSR.render was daft.

So now you just do any of these, and you're done:

view(model).render
p("Hello there!").render
p("Hello there!").toString

What about the doctype?! Well... it's just a string you prefix to your rendered HTML. Do you need me for that? :thinking:

I have provided a constant for the default...

DOCTYPE +
  html(
    head(
      title("My Page")
    ),
    body(
      p(text("Hello, world!"))
    )
  )
// String: <!DOCTYPE HTML><html><head><title>My Page</title></head><body><p>Hello, world!</p></body></html>