-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
How to use For Loop / If condition / Map with HTML FLow? #102
Comments
Hi @moorsu, Short answer. Example of if/else (Taken from https://htmlflow.org/features#getting-started): void StocksView(Appendable out, Stock stock) {
HtmlFlow
.doc(out)
.html()
….td().of(td -> { if (stock.getChange() < 0) td.attrClass("minus"); }).…
...
.__();
} Or dynamic view: HtmlView stocksView = HtmlFlow.view(view -> view
.html()
….td().dynamic<Stock>((td, stock) -> { if (stock.getChange() < 0) td.attrClass("minus"); }).…
...
.__();
); An HtmlView is more efficient, renders and store the static blocks once. Render the latter with: String html = stocksView.render(new Stock(....));
// Or:
stocksView.setOut(System.out).write(new Stock(....)); Foreach example (Taken from https://htmlflow.org/features#dynamic-views): With Dynamic View: HtmlView tasksTableView = HtmlFlow.view(page -> page
.html()
...
.table()
.attrClass("table")
.tr().th().text("Title").__().th().text("Description").__().th().text("Priority").__().__()
.tbody()
.<Stream<Task>>dynamic((tbody, tasks) ->
tasks.forEach(task -> tbody
.tr()
.td().text(task.getTitle()).__()
.td().text(task.getDescription()).__()
.td().text(task.getPriority().toString()).__()
.__() // tr
) // forEach
) // dynamic
.__() // tbody
.__() // table
....
); With HtmlDoc: void tasksTableDoc(Appendable out, Stream<Task> tasks) {
HtmlFlow
.doc(out)
.html()
...
.table()
.attrClass("table")
.tr().th().text("Title").__().th().text("Description").__().th().text("Priority").__().__()
.tbody()
.of(tbody ->
tasks.forEach(task -> tbody
.tr()
.td().text(task.getTitle()).__()
.td().text(task.getDescription()).__()
.td().text(task.getPriority().toString()).__()
.__() // tr
) // forEach
) // dynamic
.__() // tbody
.__() // table
....
} HtmlFlow is unopinionated and you can use any Java control flow you each. We do not need specific builders such as J2html Whenever you need a Java expression, you can simply chain I know that HtmlFlow documentation has some quite failures, and you can find many examples spread out in the README, getting-started, unit tests, and others. |
Thanks @fmcarvalho. I will try. Is there any discord channel for htmlflow ? |
@moorsu I am not aware of any Discord channel for HtmlFlow. Usually I try to give quick answers to all issues. I have to update HtmlFlow documentation with this examples. I will keep this Issue open until there. |
@moorsu Can you please star HtmlFlow project to raise attention from the community. Thanks |
Done. Thanks! |
I included the most common use cases for data binding, if/else, and loops usage, in GitHub README. Also in https://htmlflow.org/features I have included the same examples implemented with both approaches: |
Can you please point me an example for using for - loop / if else / using map to fill the data in htmlflow like the ones used by J2HTML?
The text was updated successfully, but these errors were encountered: