Search code examples
javahtmlj2html

How do I use j2html without rendering everything


I am converting my html rendering code to use j2html. Whilst I like the library it is not easy for me to convert all the code in one go so sometimes I may convert the outer html to use j2html but not able to convert the inner html to j2html at the same time. So I would like j2html to be able to accept text passed to it as already rendered, but it always re-renders it so

System.out.println(p("<b>the bridge</b>"));

returns

<p>&lt;b&gt;the bridge&lt;/b&gt;</p>

is there a way I get it to output

<p><b>the bridge</b></p>

Full Test Case

import j2html.tags.Text;

import static j2html.TagCreator.b;
import static j2html.TagCreator.p;

public class HtmlTest
{
    public static void main(String[] args)
    {
        System.out.println(p(b("the bridge")));
        System.out.println(p("<b>the bridge</b>"));
    }

}

Solution

  • import static j2html.TagCreator.b;
    import static j2html.TagCreator.p;
    import static j2html.TagCreator.rawHtml;
    
    
    public class HtmlTest
    {
        public static void main(String[] args)
        {
            System.out.println(p(b("the bridge")));
            System.out.println(p(rawHtml("<b>the bridge</b>")));
        }
    
    }
    

    Result:

    <p><b>the bridge</b></p>
    <p><b>the bridge</b></p>