In one of out projects I had a requirement to prepare a multiline text property that supports line breaks. It had to be a standard textarea field with no HTML elements just plain text with new line support. The dojo editor didn’t require changes, because line breaks were stored during saving property data. Only value renderer had to be implemented.
The property value was saved with line breaks, but text was displayed without using <br> tag.
Extending standard string control
To add new lines I implemented new template renderer. It extends PropertyExtendedStringControl and override ToWebString method. The method is replacing \n characters with HTML line breaks tags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using EPiServer.Framework.DataAnnotations; using EPiServer.Web; using EPiServer.Web.PropertyControls; namespace AlloyDemo { [TemplateDescriptor(TagString = "textWithLineBreaks")] public class PropertyExtendedStringControl : PropertyStringControl, IRenderTemplate<string>, IRenderTemplate { public PropertyExtendedStringControl() { } public PropertyExtendedStringControl(int maxLength) : base(maxLength) { } public override string ToWebString() { var webString = base.ToWebString(); if (string.IsNullOrWhiteSpace(webString) == false) { return webString.Replace("\n", "<br>"); } return webString; } } } |
To use new renderer property it should be added with “textWithLineBreaks” tag.
1 2 3 |
<EPiServer:Property PropertyName="TextWithLineBreak" runat="server" > <RenderSettings Tag="textWithLineBreaks"></RenderSettings> </EPiServer:Property> |
After running the code, line breaks are rendered.