Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering multiple elements in one go without parent element #136

Open
ijabz opened this issue Jul 22, 2019 · 1 comment
Open

Rendering multiple elements in one go without parent element #136

ijabz opened this issue Jul 22, 2019 · 1 comment
Labels

Comments

@ijabz
Copy link

ijabz commented Jul 22, 2019

Typically I can not create the whole html file in one go, typically I may have to create the header and main contents of the file at one point, but then finish the file creation at later date. So often this means I cannot use j2html to create the tag, because I will add the closing at a later point, this is fine but then if I have multiple elements that should attach directly to body rather than to an intermediate element I have to make multiple calls to render(), it would be nice if I could do once.

i.e I want output to be

<body>
<h1>..</h1>
<h2>..</h2>
....

so I have to render h1 and h2 separately

Alternatively I could output

<body>
<div>
<h1>..</h1>
<h2>..<./h2>
</div>

and then I could just render div, but now I am modifying my html just to suit j2html

Would be nice if I could render i one go/store the elements as a special list that j2html could render accordingly

Real code extract

StringBuilder sb = new StringBuilder();
            sb.append(BootstrapReports.getReportPageSongChangesFilesStart(FixSongsController.isPreview(), InfoMessage.MSG_FIX_SONGS_REPORT_TITLE.getMsg(), FixSongsReport.getInstance().getMenuItems()));
            sb.append(DIV_CLASS_CARD_STYLE_BORDER_0_DIV_CLASS_CARD_BODY);

sb.append(h1(InfoMessage.MSG_FIX_SONGS_REPORT_TITLE.getMsg() + " " + currentReportId).renderFormatted());
sb.append(h2(title + ": " + artistCredit).renderFormatted());
@pointbazaar
Copy link
Contributor

pointbazaar commented Aug 14, 2020

You do not need multiple calls to .render(), even when you build your html in increments.

You just need to reverse the order of creating the elements. The innermost elements have to be created first,
then the outermost elements, and then you can render it all in one call.

Example:

var t1 = h1(InfoMessage....);
var t2 = h2(title + ": "+ artistCredit);

return body(t1,t2).render()

This way you avoid using StringBuilder and also avoid the extra <div>.

Alternative (using Array to store elements):

var arr = new ContainerTag[]{h1(...), h2(...)};
return body(arr).render()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants