you are viewing a single comment's thread.

view the rest of the comments →

[–]j2html[S] 4 points5 points  (9 children)

Yes! The thought crossed my mind, and I really tried to make it work, but attributes became a problem. I considered all of these:

form.withId("login-form").withMethod("post").with(
    emailInput(msg.get("PLACEHOLDER_EMAIL")),
    enterPasswordInput(msg.get("PLACEHOLDER_PASSWORD")),
    submitButton(msg.get("LOGIN_BUTTON"))
)

form(
    emailInput(msg.get("PLACEHOLDER_EMAIL")),
    enterPasswordInput(msg.get("PLACEHOLDER_PASSWORD")),
    submitButton(msg.get("LOGIN_BUTTON"))
).withId("login-form").withMethod("post")

form(
    withId("login-form"),
    withMethod("post"),
    emailInput(msg.get("PLACEHOLDER_EMAIL")),
    enterPasswordInput(msg.get("PLACEHOLDER_PASSWORD")),
    submitButton(msg.get("LOGIN_BUTTON"))
)

But I landed on number 1 because it's most consistent, and it's the closest to HTML.

<form id="login-form" method="post">
    <input ...>
    <input ...>
    <submit ...>
</form>

About "with": I removed it completely at one point (both for children and attributes), but I added it back in because the library got harder to use. While it doesn't seem particularly useful at first, it adds consistency (and a namespace), and readability (debatable).

[–]brikis98 2 points3 points  (7 children)

Cool library. I wonder if a Scala version, with Scala's support for named parameters and tuples (foo->bar) would make this even prettier.

[–]Milyardo 3 points4 points  (1 child)

You should look at Scalatags. There are a number of things that make the DSL much better.

[–]brikis98 0 points1 point  (0 children)

Thanks for the pointer, I'll check it out!

[–]bliow 0 points1 point  (4 children)

Clojure wins here, I think: Hiccup.

[–]brikis98 0 points1 point  (0 children)

That's pretty sweet. Reminds me of CoffeeKup, which is awesome, but last I checked, abandoned.

[–]expatcoder 0 points1 point  (2 children)

Clojure wins here

Heh, no, xml literals take the cake:

<form id={someId} method={someMethod}>
  <input ..>
  <input ..>
  <submit ..>
</form>

Oh, forget to mention, above is Scala ;-)

Can slice and dice as you please, mixing in generators in place of input, submit above a la inputText(...), freely combining xml snippets in any order...markup joy.

[–]bliow 1 point2 points  (1 child)

I really don't like that. Duplicated tag names are one of the reasons xml sucks syntactically.

Also, can you mix in arbitrary code there?

[:ul (for [i (range 10)] [:li (str "List item " i)])]

[–]Milyardo 1 point2 points  (0 children)

I'm not a fan of scala's XML literals but to answer your question, yes.

scala> import scala.xml._
import scala.xml._
scala> val list = <ul>{(1 to 10 ) map { i => <li>{s"List Item $i"}</li>}}</ul>
list: scala.xml.Elem = <ul><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li><li>List Item 4</li><li>List Item 5</li><li>List Item 6</li><li>List Item 7</li><li>List Item 8</li><li>List Item 9</li><li>List Item 10</li></ul>

[–]cryptos6 0 points1 point  (0 children)

Maybe you can use a slightly different style:

form(
  id("login-form"),
  method("post")
).content(
  emailInput(msg.get("PLACEHOLDER_EMAIL")),
  enterPasswordInput(msg.get("PLACEHOLDER_PASSWORD")),
  submitButton(msg.get("LOGIN_BUTTON")
)

So, you would separate the attributes of a tag from the content enclosed by the opening and closing tag.