tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.
https://j2html.com/
Apache License 2.0
765 stars 137 forks source link

Rendering with data #83

Closed kicktipp closed 6 years ago

kicktipp commented 6 years ago

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.

kicktipp commented 6 years ago

See here for changes which are needed: https://github.com/tipsy/j2html/compare/master...kicktipp:render-with-model?expand=1

tipsy commented 6 years ago

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.

tipsy commented 6 years ago

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

kicktipp commented 6 years ago

I will need a few weeks to evaluate it. Release fast, release often. So go ahead.

Am 27. November 2017 19:45:06 schrieb David notifications@github.com:

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

-- You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: https://github.com/tipsy/j2html/issues/83#issuecomment-347283382