question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improvement: invisible parent tag

See original GitHub issue

I came across a situation in which I need something like an invisible parent to generate something like this:

<ul>
    <li>First</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>Last</li>
</ul>

This structure is often used in generating paginating bars. The part from 5 to 7 is generated.

In similar situations, you would an extra div:

ul().with(
    li("First"),
    div().with(
        numbers.stream().map(number ->
            li(number.toString())
        ).collect(Collectors.toList())
    ),
    li("Last")
)

But a div directly inside a UL-element is unwanted.

Another option (without creating new classes):

ul().with(
    li("First"),
    unsafeHtml(
        numbers.stream().map(number ->
            li(number.toString()).render()
        ).collect(Collectors.joining(""))
    ),
    li("Last")
)

But the use of unsafeHtml(), render() and joining(“”) doesn’t feel very nice.

Another option would to to create something like an invisible parent tag, a tag that doesn’t show its own element, only its children:

ul().with(
    li("First"),
    group(
        numbers.stream().map(number ->
            li(number.toString())
        ).collect(Collectors.toList())
    ),
    li("Last")
)

This can further be improved with a custom java.util.stream.Collector that internally used the group(), so that group() doesn’t have to be exposed:

ul().with(
    li("First"),
    numbers.stream().map(number ->
        li(number.toString())
    ).collect(domContentCollector()),
    li("Last")
)

This last one looks very nice imo, with the domContentCollector().

But solution does require Java 1.8 instead of 1.7.

Code: https://github.com/jtdev/j2html/commit/bf7e3ac5bd0aa2de256c91070709ad69d9220ddb (Not fully sure whether DomContentCollector is implemented completely correct.)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
devxzerocommented, Apr 25, 2016

@kosiakk Using variables breaks the readability of the html and fluent api imo.

Mixing DomContent with Lists gives a loss of type safety.

And the stream() with .collect(domContentCollector()) approach is a bit verbose, although flexible. each(collection, mapper) and filter() are a nice short solutions although not sure if they fit all use cases. each(stream, mapper) would fit more use cases, although slightly more verbose.

About if, I created an issue for it here: https://github.com/tipsy/j2html/issues/21 if is maybe even more difficult than each in terms of discovering a nice api.

Btw, speaking about Kotlin, Scala also has a HTML builder: Scalatags: http://www.lihaoyi.com/scalatags/ In Clojure (also a JVM language), they have Hiccup: https://github.com/weavejester/hiccup

1reaction
tipsycommented, Apr 21, 2016

Nice work! I was playing around a little, and I really think the stream API is too verbose. I created an alternative suggestion:

li("First"),
each(numbers, number -> li(number.toString())),
li("Last")

Made possible by a helper like this:

DomContent each(Collection<?> collection, Function<Object, DomContent> mapper) {
    return collection.stream().map(mapper).collect(domContentCollect());
}

But it won’t really work if you need to do something like employee.getName(). Feels like it should be possible, but I’m still pretty new to functions/lambdas/generics/wildcards.

Read more comments on GitHub >

github_iconTop Results From Across the Web

parent hidden but children still visible (I don't want them visible)!
Any child elements that are added post page-load seem to have somehow lost their inheritance link with the parent DIV element (the red...
Read more >
Making Child Element Visible When Outside Parent Element ...
However, sometimes, it ends by putting absolutely the element outside of the ridiculous overflow: hidden parent, and you cursing CSS that how it...
Read more >
Rustdoc cleanups and improvements - Blog - Guillaume Gomez
We recently improved a lot of things in rustdoc. However, these changes are mostly invisible for users even though they're making their ...
Read more >
[2209.03387] A new approach to semi-leptonic tags in $B - arXiv
One general challenge in these decays arises if both the signal parent and the 'other' parent, often used as a tag, decay semi-invisibly....
Read more >
Invisible Sentence: Recognizing, Supporting, and Advocating ...
... about children of incarcerated and returning parents and work to create a ... and tools to advocate for these children to improve...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found