When creating components in Sitecore, we try to make them as manageable from the Experience Editor as possible, but there are times when we have to give additional instructions so that the Content Author can use the placeholders properly, add options, modify content, etc.
In this article, we'll see how to use a helper to display a special message for the Experience Editor, this could be an informative or warning one.
Helper code
In this code we will see two methods to display a message in the Experience Editor, validating that it is in that mode according to the context, and with special styles.
public static class ExperienceEditorMessage
{
public static HtmlString Info(string message)
{
return !Context.PageMode.IsExperienceEditorEditing
? new HtmlString(string.Empty)
: new HtmlString($"<div style=\"background: #AADBEE; margin: 10px 0px; padding: 10px 8px; border-radius: 8px; font-size: 12px;\"><b style=\"font-weight: bold;\">Info</b>: {message}</div>");
}
public static HtmlString Warning(string message)
{
return !Context.PageMode.IsExperienceEditorEditing
? new HtmlString(string.Empty)
: new HtmlString($"<div style=\"background: #f8f8a0; margin: 10px 0px; padding: 10px 8px; border-radius: 8px; font-size: 12px;\"><b style=\"font-weight: bold;\">Warning</b>: {message}</div>");
}
}
Example from a view
From the view of a component, this way we can display the expected message.
@ExperienceEditorMessage.Info("This is an Experience Editor message for general information.")
@ExperienceEditorMessage.Warning("This is an Experience Editor warning message.")
This is the result in the Experience Editor:
Thanks for reading!
And that's it! With this proposed solution we can show a special message in the Experience Editor for Content Authors. If you have any questions or ideas in mind, it'll be a pleasure to be able to be in communication with you, and together exchange knowledge with each other.
Top comments (0)