Release 4.0 Support for Asynchronous Models
· 2 min read
This release introduces the following changes when migrating from HtmlFlow 3.x to 4.x:
DynamicHtmlandStaticHtmlare replaced byHtmlViewandHtmlDoc, respectively. Both inherit from the newHtmlPagetype.- The factory methods
DynamicHtml.view()andStaticHtml.view()have been moved to theHtmlFlowclass, becomingHtmlFlow.view()andHtmlFlow.doc(). - The
HtmlWriterinterface, which previously defined standard methods (writeandrender) for all HTML pages, has been removed. With the dynamic nature ofHtmlView(formerlyDynamicHtml),render(model)andwrite(model)now require a model argument. These methods are not applicable toHtmlDoc(formerlyStaticHtml), leading to the removal ofHtmlWriter. The base classHtmlPageno longer defines write or render methods. HtmlPage,HtmlView, andHtmlDocare no longer parameterized with the model type. The model is now parameterized only when using the builder<M> dynamic(BiConsumer<E, M> consumer), whereEis the parent HTML element andMis the model type.- The model object is now passed directly as a parameter to the consumer of a dynamic block (e.g., calling
.dynamic((elem, model) -> ...)), eliminating the need for closures to capture the model object.
For example, the dynamic blocks approach has been simplified:
- HtmlFlow 3.x API
- HtmlFlow 4.x API
Previous 3.x approach
public void template(DynamicHtml<Pet> view, Pet pet) {
view.div().h2().dynamic(h2 -> pet.getName()).....
...
New 4.x approach
public void template(HtmlPage view) {
view.div().h2().<Pet>dynamic((h2, pet) -> pet.getName()).....
...
- Partials are now the only place where closures can capture a model object of a different type than the enclosing template. When doing so, you must use a
dynamic()block to prevent the partial from being stored as static HTML and ensure it renders whenever called with a model. If the partial uses the same model as its container, avoid the closure and instead use the model provided to thedynamic(cons)builder. - The
addPartial()builder has been removed. A partial is now simply a consumer function of an HTML element (Consumer<E extends Element>). You can combine partials using higher-order function composition. The element serves as the parent HTML where HtmlFlow continues to emit code. Additional arguments corresponding to context models can also be passed to partial views.