Rendering rich text
Brightspot stores rich-text elements as String
s, and a ViewModel's getter for a String
retrieves the text as it is stored. If Mary says to Henry in plain text, "No, Henry, I simply will not marry you!", Brightspot stores her refusal as
No, Henry, I simply will not marry you!
and the view renders it the same way.
However, if Mary wants to put some emphasis into her refusal, she may say,
No, Henry, I simply will not marry you!
To render the previous string with boldface and italics, Brightspot needs to store HTML markup along with the plain text as in the following example:
No, Henry, I simply <i>will not</i> marry <b>you<b>!
However, Brightspot returns strings as they are stored. If you apply a simple getter to the above string, Brightspot escapes the opening and closing brackets so they appear in the rendered page. Looking at the rendered string's source, you see all the escape codes.
No, Henry, I simply <i>will not</i> marry <b>you</b>!
Brightspot provides the RichTextViewBuilder class to revert the escaped HTML to unescaped HTML so that the browser renders the rich text as expected.
Step 1: Create a rich-text marker
Create a rich-text marker interface RichTextElementView
.
public interface RichTextElementView { }
Brightspot uses implementations of this interface to create Views.
Step 2: Add rich-text support to view model
Apply the method RichTextViewBuilder#buildHtml
to the escaped HTML.
import com.psddev.cms.rte.RichTextViewBuilder; import com.psddev.cms.view.ViewModel; import com.psddev.cms.view.PageEntryView; import styleguide.content.article.ArticleView; public class ArticleViewModel extends ViewModel<Article> implements ArticleView, PageEntryView { @Override public CharSequence getBody() { return RichTextViewBuilder.buildHtml( model.getBody(), 1 e -> createView(RichTextElementView.class, e)); 2 } /* Other getters */ }