I'm creating a custom dialect that replaces <myprefix:mytag> with a <div th:replace="myTagFragment::myfrag"> and it works with no attributes or "standard" attributes.
It stops working if I use th: attributes in the custom tag.
This works:
<myprefix:mytag required id="myid">
This does not work:
<myprefix:mytag th:field="*{name}">
TagProcessor code snippet:
protected void doProcess(ITemplateContext context, IModel model, IElementModelStructureHandler structureHandler) {
final IOpenElementTag customTag = (IOpenElementTag) model.get(0);
//
// I create a comma-separated list of the original attributes and add them in the fragment with th:attr
Map<String, String> attributes = sourceTag.getAttributeMap();
String attributeString = attributes.toString(); // "{attr1=val1, attr2=val2}"
attributeString = StringUtils.chop(attributeString); // Remove "}"
attributeString = StringUtils.removeStart(attributeString, "{"); // Remove "{"
structureHandler.setLocalVariable("attributeString", attributeString); // "attr1=val1, attr2=val2"
//
Map<String, String> newtagAttributes = new HashMap<>();
newtagAttributes.put("th:replace", "myTagFragment::myfrag");
IOpenElementTag replacementTag = modelFactory.createOpenElementTag("div", newtagAttributes, AttributeValueQuotes.DOUBLE, false);
model.replace(0, replacementTag);
Fragment snippet from "myTagFragment.html":
<th:block th:fragment="myfrag">
<div>somestuffhere</div>
<input th:attr="__${attributeString}__">
When I use
<myprefix:mytag type="password" maxlength="70" id="titolo">
I correctly get the replaced fragment:
<div>somestuffhere</div>
<input type="password" maxlength="70" id="titolo">
but if I want to use th: attributes in the original tag, they fail because something like
th:field is not processed properly when added with th:attr="__${attributeString}__" of course.
Maybe there is a way to retrieve the fragment model from inside the TagProcessor and add the th: attributes by code? Any other idea to replace a custom tag with a template fragment while keeping the original attributes and have them processed when needed?