tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.

Home Page:https://j2html.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nbsp in each - java

Sam-Levene opened this issue · comments

I have the following code in Java that I want to implement:

each(suiteMeta.getGroups(), group ->
        span().withText(group).withClasses("badge badge-info"),
        text(" ")
)

whereby the HTML would read as follows:

<span class="badge badge-info">group</span>
&nbsp;
<span class="badge badge-info">group</span>
&nbsp;

etc;

or something similar whereby I am using Twitter Bootstrap to create pill-icons for each span element, then for each element, I also want a non-breaking space between the spans (Not inside of them)

However, as you'd expect, my code complains at me saying:

Error:(91, 37) java: no suitable method found for each(java.util.List<java.lang.String>,(group)->s[...]nfo"),j2html.tags.ContainerTag)
    method j2html.TagCreator.<I,T>each(java.util.Map<I,T>,java.util.function.Function<java.util.Map.Entry<I,T>,j2html.tags.DomContent>) is not applicable
      (cannot infer type-variable(s) I,T
        (actual and formal argument lists differ in length))
    method j2html.TagCreator.<T>each(java.util.Collection<T>,java.util.function.Function<? super T,j2html.tags.DomContent>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))

Am I doing something wrong here? There's no support in the documentation

commented

That's very strange, let me have a look.

commented

Okay, not so strange. You have one too many arguments to each.
You can use a span to combine your two args:

public static void main(String[] args) {
    String value = each(Arrays.asList("1", "2", "3", "4"), group ->
        span(
            span().withText(group).withClasses("badge badge-info"),
            rawHtml("&nbsp;")
        )
    ).render();
    System.out.println(value);
}

Since you want to use &, you should also use rawHtml instead of text, as text would escape the character.

@tipsy - Thanks for this, it's very much appreciated, nbsp had me really confused. Perhaps you could add a section in the support docs for rawHTML such as nbsp?