Class ThymeleafModelBuilder
java.lang.Object
com.foreach.across.modules.web.thymeleaf.ThymeleafModelBuilder
Specialized builder class wrapping around a Thymeleaf
IModelFactory.
Meant for writing HTML style elements.
When an element is opened, its attributes can be modified until it is flushed to the Thymeleaf model.
Flushing happens when either a new element is opened, or the current element is closed.
An attribute can have multiple values, these will be joined together with a DOUBLE space character.- Since:
- 2.0.0
- Author:
- Arne Vandamme
-
Constructor Summary
ConstructorsConstructorDescriptionThymeleafModelBuilder(@NonNull org.thymeleaf.context.ITemplateContext templateContext, @NonNull ViewElementModelWriterRegistry nodeBuilderRegistry, @NonNull HtmlIdStore htmlIdStore, @NonNull ViewElementAttributeConverter attributeConverter, @NonNull com.foreach.across.modules.web.thymeleaf.AttributeNameGenerator attributeNameGenerator, boolean developmentMode) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttribute(String attributeName, Object... values) Set the attribute with the given name.voidaddAttributes(Map<String, Collection<Object>> attributes) Adds all attributes to the current open element.voidaddAttributeValue(String attributeName, Object... values) Add values for a specific attribute.voidaddBooleanAttribute(String attributeName, boolean value) Add a boolean attribute.voidCloses the current element, flushes it to the model.voidAdd some HTML to the model.voidaddModel(org.thymeleaf.model.IModel childModel) Directly add a child model to the currentIModelthis builder represents.voidaddOpenElement(String tagName) Start a new element with the specific tag name.voidAdd some text to the model.voidAdd some text to the model.voidaddViewElement(ViewElement viewElement) Add the element to the model.voidchangeOpenElement(String tagName) Change the open element to the specific tag.org.thymeleaf.model.IModelcreateViewElementModel(ViewElement viewElement) Create a separateIModelfor aViewElement.org.thymeleaf.model.IModelFactoryorg.thymeleaf.context.ITemplateContextvoidremoveAttribute(String attributeName) Remove a single attribute for the open element.voidRemoves all currently declared attributes for the open element.voidremoveAttributeValue(String attributeName, Object... values) Remove one or more values for a particular attribute.retrieveHtmlId(ViewElement viewElement) Get a unique id for the specific element.org.thymeleaf.model.IModelReturn the current model, ensures pending tags have been written.
-
Constructor Details
-
ThymeleafModelBuilder
public ThymeleafModelBuilder(@NonNull @NonNull org.thymeleaf.context.ITemplateContext templateContext, @NonNull @NonNull ViewElementModelWriterRegistry nodeBuilderRegistry, @NonNull @NonNull HtmlIdStore htmlIdStore, @NonNull @NonNull ViewElementAttributeConverter attributeConverter, @NonNull @NonNull com.foreach.across.modules.web.thymeleaf.AttributeNameGenerator attributeNameGenerator, boolean developmentMode)
-
-
Method Details
-
getTemplateContext
public org.thymeleaf.context.ITemplateContext getTemplateContext()- Returns:
- Thymeleaf template context
-
getModelFactory
public org.thymeleaf.model.IModelFactory getModelFactory()- Returns:
- Thymeleaf model factory
-
retrieveHtmlId
Get a unique id for the specific element. Takes into account the id property set if it is aHtmlViewElementbut will ensure duplicates return a unique value.- Parameters:
viewElement- to get a unique id for- Returns:
- unique id
-
addViewElement
Add the element to the model. Will lookup theViewElementModelWriterfor the element type in theViewElementModelWriterRegistryattached to this model builder. If the request contains aWebTemplateInterceptor.RENDER_VIEW_ELEMENTattribute, it is considered to be the name of the ViewElement that should have its actual output rendered. All other ViewElements will still get built, but their markup suppressed. In case of a partial rendering, special processing instructions are added to tell thePartialViewElementTemplateProcessorto allow the markup.- Parameters:
viewElement- to add
-
createViewElementModel
Create a separateIModelfor aViewElement. The model will use the configuration of thisThymeleafModelBuilderbut will not yet been added. This method is useful if you want to manually post-process a model before adding it. Adding the child model can be done throughaddModel(IModel).- Parameters:
viewElement- to create the model for- Returns:
- model
-
createChildModelBuilder
- Returns:
- a separate model builder using the same configuration as the current one
-
addModel
public void addModel(org.thymeleaf.model.IModel childModel) Directly add a child model to the currentIModelthis builder represents.- Parameters:
childModel- to add
-
retrieveModel
public org.thymeleaf.model.IModel retrieveModel()Return the current model, ensures pending tags have been written.- Returns:
- model
-
addText
Add some text to the model. Text will be escaped.- Parameters:
text- to add
-
addHtml
Add some HTML to the model.- Parameters:
html- to add
-
addText
Add some text to the model.- Parameters:
text- to addescapeXml- true if text should be escaped
-
addAttributes
Adds all attributes to the current open element. Will throw an exception if no element is currently opened and still available for modification. Any other attributes will remain, but attributes with the same name will be replaced if there is at least one valid value for that attribute.- Parameters:
attributes- to set- See Also:
-
addBooleanAttribute
Add a boolean attribute. A boolean attribute is an attribute that is either present or not, its value is determined by its presence. In regular html for example this would be written asrequired="required". This method will add the attribute if istrue. Any existing attribute with that name will be replaced, or removed if isfalse. Requires an open element.- Parameters:
attributeName- name of the attributevalue- true if it should be added
-
addAttribute
Set the attribute with the given name. Will replace any existing attribute values. If is empty, a single value identical to will be added. If is not empty but contains onlynull, the attribute will be ignored and any previously registered value will be kept. All values will be XML escaped and duplicate values will be ignored. Requires an open element.- Parameters:
attributeName- name of the attribute to setvalues- to set for the attribute
-
addAttributeValue
Add values for a specific attribute. Any other values will remain. Values will be XML escaped and duplicate values will be ignored. Requires an open element.- Parameters:
attributeName- name of the attribute to modifyvalues- to add to the attribute
-
removeAttribute
Remove a single attribute for the open element. Requires an open element.- Parameters:
attributeName- name of the attribute to remove
-
removeAttributes
public void removeAttributes()Removes all currently declared attributes for the open element. -
removeAttributeValue
Remove one or more values for a particular attribute. If no values remain, the entire attribute will be removed. Requires an open element.- Parameters:
attributeName- name of the attribute from which you want to remove some valuesvalues- to remove
-
addOpenElement
Start a new element with the specific tag name. After the element is opened, attributes can be modified until it is flushed.- Parameters:
tagName- of the element
-
changeOpenElement
Change the open element to the specific tag. Will throwIllegalStateExceptionif there is no currently open element that has not yet been flushed.- Parameters:
tagName- of the element
-
addCloseElement
public void addCloseElement()Closes the current element, flushes it to the model.
-