I use DXA JAVA 1.4's <dxa:richtext> tag to output a richtext field. I would like to ensure that the content from this richtext fields is always wrapped in a <p> paragraph. Can DXA handle this for me, adding the <p> when needed, omitting it when the content already contains one or more <p> tags?

And actually, is the webapplication the rigt place to handle this, or should the Content Manager enforce to wrap the content?

| improve this question | | | | |
  • 1
    I think the two best-fitted options here is to "bake" the <p> either in the stored content (XSLT filter or event) or during publish. In terms of editorial control, pressing ENTER should add the tag ("training" is another solution). I think I asked how others solved this somewhere, but I can't find it right now. – Alvin Reyes May 31 '16 at 14:00
  • I think the DXA dev team clearly showed its opinion through all the answers, but please do not let that stop you in logging an enhancement request for this if you feel this is something the web application should handle. – Bart Koopman Jun 3 '16 at 10:38

DXA will currently not handle this for you and indeed you are asking the right question, where should this be handled?

Since the RTF field is rich text, I personally don't think the web application should interfere with its content, and simply write them out as is (links in it are resolved and the output is formatted).

Simply wrapping its content in a <p> tag is not trivial, since it could be wrapped in a <div> tag, or even worse it could contain a <div> tag somewhere in the middle, in which case it would invalidate the HTML, since a <p> tag is a block level element, and it can only contain inline elements.

I have attempted an XSLT for the RTF field once to handle this, but also run into the problems of so many edge cases that I think I gave up in the end.

| improve this answer | | | | |

Basically <dxa:richtext content="instance of RichText"> iterates over fragments of HtmlElement passed into as a parameter, and doesn't take care of wrapping anything.

The tag calls richText.getFragments() to get those. So you can create a subclass of RichText and add this wrapping logic to a new class, and then pass instance of it to the tag.

And actually, is the webapplication the rigt place to handle this, or should the Content Manager enforce to wrap the content?

I would rather say no since CM is something that should be managing content. At the same time, this depends on your and your app goals. If this is nothing related to content itself but e.g. to your JavaScript or so, then this might be managed by webapp. DXA should load content from and just render it to show exactly what is on CM.

| improve this answer | | | | |

The Type RichText and associated <dxa:richtext> tag are intended to render Rich Text with embedded Media Items in an elegant manner (i.e. by letting the embedded Media Items be rendered using their own Views).

Alternatively, you can let the View Model property be of type string. In that manner, the Rich Text HTML (incl. embedded Media Item HTML) is rendered as part of the model mapping process and stored in the string property.

This allows your View code to do post-processing of the HTML (BTW: in the .NET implementation you can do that even if you use type RichText and the associated @Html.DxaRichText helper, because that just returns a string)

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.