In recent versions of Sitecore – at least 8.2 and some versions of 8.1 – line breaks in multi-line fields are not rendered as <br/>
tags when output on a page although they are supposed to.
@Html.Sitecore().Field("Multi line field")
When outputting fields like above it runs the renderField
pipeline to get the content of the field.
You can force the correct behavior adding the linebreaks
parameter.
@Html.Sitecore().Field("Multi line field", new { linebreaks = "<br/>" })
This will now work as expected and render all line breaks as the value of that parameter – <br/>
. You can use anything you like here. When the parameter is not there, it is supposed to fall back to using <br/>
, but it currently doesn’t.
Sitecore is tracking this bug by reference number 95002.
Workaround/solution
It’s pretty easy to fix though. One way (as the supplied fix by Sitecore) is to replace the GetMemoFieldValue
processor in the renderField
pipeline with a fixed version, which falls back to using <br/>
if the linebreaks parameter is null
.
public class GetMemoFieldValue
{
public void Process(RenderFieldArgs args)
{
switch (args.FieldTypeKey)
{
case "memo":
case "multi-line text":
string linebreaks = args.RenderParameters["line-breaks"]
?? args.RenderParameters["linebreaks"])
?? "<br/>";
args.Result.FirstPart = GetMemoFieldValue.Replace(args.Result.FirstPart, linebreaks);
args.Result.LastPart = GetMemoFieldValue.Replace(args.Result.LastPart, linebreaks);
break;
}
}
private static string Replace(string output, string linebreaks)
{
output = output.Replace("\r\n", linebreaks);
output = output.Replace("\n\r", linebreaks);
output = output.Replace("\n", linebreaks);
output = output.Replace("\r", linebreaks);
return output;
}
}
Then you just replace the existing GetMemoFieldValue
processor with the fixed on with a config patch.