HtmlFlow icon indicating copy to clipboard operation
HtmlFlow copied to clipboard

Can we use If Else block in Dynamic Html? (To do: include it in Documentation)

Open ZinfinityDarshan opened this issue 5 years ago • 3 comments

I am using HTML Flow in our project at Zinfinity, can you please tell me if we can use If Else block in HTML ?

ZinfinityDarshan avatar Jul 24 '20 06:07 ZinfinityDarshan

Certainly you can. HtmlFlow is unopinionated and provides you several ways of doing it. Btw you can do it both in Dynamic and Static HTML.

You may find many examples of different kinds of use in Pet Clinic sample application in org/springframework/samples/petclinic/views folder here: https://github.com/xmlet/spring-petclinic/tree/master/src/main/java/org/springframework/samples/petclinic/views

For instance, in CreateOrUpdateOwnerForm.java you have a submit button , which contains the label Add Owner or Update Owner depending on whether the view is returned from the /owners/new or /owners/ownerId/edit path. To that end, you may find the following snippet in line 33:

.button().attrClass("btn btn-default").attrType(EnumTypeButtonType.SUBMIT)
    .dynamic(bt -> {
        if(owner == null) bt.text("Add Owner");
        else bt.text("Update Owner");
    })
.__() //button

Simply put, dynamic(elem -> ...) or of(elem -> ...) let you chain any control flow logic with the last created element passed to the lambda. In the previous example the element it is the button bt.

The main difference between of() and dynamic() is that the former will keep the resulting HTML in cache and only evaluates the condition on first resolution/rendering. Whenever your condition depends of dynamic data, like a data model, then you should use dynamic().

I hope it helps.

fmcarvalho avatar Jul 24 '20 09:07 fmcarvalho

This should be added to the documentation. I had to look through many issues before I found this answer.

volgin avatar Nov 12 '21 17:11 volgin

Thanks @volgin your feedback is important.

Unfortunately I am overwhelmed and planning to enhance documentation for more than on year. But I did not have time yet.

I have a lot of information spread on different articles, issues, README, wiki...

I will reopen this Issue to remember that I should include some patterns and idioms in HtmlFlow documentation.

BTW this paper https://gamboa.pt/img/my-papers/lnbip2020-unopinionated-templates.pdf put side by side some templating idioms between Thymeleaf and HtmlFlow. Maybe it could give a help in other issues.

fmcarvalho avatar Nov 12 '21 18:11 fmcarvalho

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: HtmlDoc and HtmlView

fmcarvalho avatar Dec 28 '23 10:12 fmcarvalho

@ZinfinityDarshan @volgin As you may be aware, I am the author and primary advocate for the HtmlFlow Java library. As part of my continuous endeavors, I am required to provide justification for this project to my university.

HtmlFlow serves as a versatile auxiliary tool utilized across various sectors, facilitating software development by streamlining tasks such as HTML reporting automation, web templating, and more.

If you have utilized HtmlFlow, I kindly request your testimony regarding my dedication to addressing issues, implementing new features, or otherwise aligning with your requirements and/or your company within the scope of a specific project, task, or work that utilized HtmlFlow during a certain timeframe.

Your testimony will play a crucial role in demonstrating my work to my university.

Please feel free to send your testimonial to my email address at ISEL: [email protected]

Thank you in advance for your attention and assistance.

fmcarvalho avatar Mar 21 '24 15:03 fmcarvalho