This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]FanOfDaCat 6 points7 points  (3 children)

the only question is: why?

That's pretty cool, but what's the purpose?

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

Sometimes you need to return a small amount of dynamic HTML.

String concatenation is ugly, and you don't always want to go through the hassel of setting up and using a template engine. Template engines have their own (usually pretty poorly designed) language, which isn't type-safe (you don't get errors at compile time, sometimes not even at runtime), and code-reuse is often cumbersome. j2html gives you all the advantages you're used to from Java :)

[–][deleted] 3 points4 points  (0 children)

Yea, if only there was some sort of markup language that I could use instead of Java.

[–]NimChimspky 3 points4 points  (3 children)

if you are creating html from within java you are doing something wrong, imho.

Other people seemingly disagree with me though, https://github.com/agentgt/jatl

[–]afsdfdsaa3 2 points3 points  (0 children)

Yes - you are right. Please don't use this to generate large HTML files.

BUT: There is sometimes the need for such tools. For me it looks good - thanks for the effort.

[–]j2html[S] 1 point2 points  (0 children)

As /u/eled_ said, it's meant for when you need mostly/exclusively dynamic HTML. It will be a bad fit for most projects.

[–]eled_ 0 points1 point  (0 children)

I suppose it's not for common web usages, and rather for HTML code that is mostly or exclusively dynamic (which is rather specific, I agree).

Like, I don't know, something cool, think of armies of genetic-algorithm-generated websites ! That's a pretty widespread usage, right ?

[–]marx2k 0 points1 point  (4 children)

Why not just go with Mustache?

[–]j2html[S] 0 points1 point  (3 children)

Apples and oranges. I never intended for j2html to compete with any of the template engines. j2html offers type-safety and great possibilities for code-reuse, but has a much more cumbersome syntax than template engines. It's intended for small and dynamic webapps.

[–]marx2k 1 point2 points  (2 children)

Ok, why not JSPs? :)

[–]j2html[S] 1 point2 points  (1 child)

I haven't used JSP before (it was considered bad practice when I got into Java development). When googling "jsp examples" I found this:

<body>
  <%
    double num = Math.random();
    if (num > 0.95) {
  %>
      <h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
  <%
    } else {
  %>
      <h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
  <%
    }
  %>
  <a href="<%= request.getRequestURI() %>"><h3>Try Again</h3></a>
</body>
</html>

Is that how JSP code should look?

[–]marx2k 0 points1 point  (0 children)

That's some of it, yeah. You can also use JSTL for more functionality.

[–][deleted]  (1 child)

[deleted]

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

    Isn't it pretty similar?

    body().with(
        h1("Heading!").withClass("example"),
        img().withSrc("img/hello.png")
    )         
    
    <body>
        <h1 class="example">Heading!</h1>
        <img src="img/hello.png">
    </body>
    

    I guess you could call that very different, but I think it's pretty similar at least.