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

Rendering with data

kicktipp opened this issue · comments

j2html is great, easy and fast. But I think it could be even faster and consume less memory with the following approach. Maybe it is not a good idea so I am asking for feedback here first.

Take a look at this code:

String headline = "My Headline";
String imgSrc = "img/hello.png";
body().with(
    h1(headline).withClass("example"),
    img().withSrc(imgSrc)
).render(); 

This template generates the DomContent each and every time I render this page. This involves - with a bigger template - a lot of new ContainerTag statements (which are hidden by static imports).

It would be nice to build a DomContent Tree with some self written ContainerTags first and then let it render with a model.

Therefor we need a second render method render(Object model). The model is passed down the line and each Tag or Attribute can pick up its model if needed for rendering. Object would usually be a map like in Spring MVC.

This way I can reuse the DomContent for each and every page. I don't need to build it and I don't need the GC to collect it.

This would be fairly easy when we add two methods to DomContent (and one method to Attribute)

public abstract String render(Object model) ;
public void render(Appendable writer, Object model);

If you think this is a good idea I can send a patch request.

commented

I'm (pleasantly) surprised that you need this kind of performance. If you can provide a backwards compatible implementation with some test/examples to show the increased performance, I would be happy to merge it.

commented

@kicktipp I'm planning to release a new version soon, do you want this included?