Is there a way to use a rich text field as meta tags?

I'm picking up where someone left off and they are trying to use a Html.Partial to load a cshtml file that pulls from a rich text field.

This is the line that calls the cshtml on the page


Here is what the MetaData.cshtml has

@model RenderingModel
@{  WriteLiteral(Model.Item.Fields["MetaDataContent"].ToString()); }
  • Using a Rich text field as for any meta tags, is bad practice, because you could potentially end up with html tags in the attribute for the meta tags, which wouldn't be good. Jan 29 '18 at 20:13
  • Also it's not a good idea to pull MetaData.cshtml into your layout via a partial. You should use @Html.Sitecore().Rendering("{guid}") instead, so you can add caching opportunities to your renderings. Jan 29 '18 at 20:16

I would be surprised if the MetaDataContent field in Sitecore is a Rich Text field, I would expect that it should more likely be a Multiline Text, perhaps with the value of the field containing the exact meta text (including tags) that should be output:

<meta name="description" content="Lorem ipsum dolor sit amet, consectetur adipiscing elit.">
<meta name="keywords" content="Lorem, ipsum, dolor, sit, amet">

The rich text field will not show the meta tags in Design view, the user would have to switch to the HTML view. You also don't want the overhead and potential misuse of the formatting options.

That said, generally speaking you should make this more user-friendly for your editors and provide them with multiple (single-line text) fields to allow them to enter the metadata values, rather than also having to explicitly type out the meta tag elements as well, for example:

<meta name="description" content="@Model.Item.Fields["MetaDataDescription"].Value">
<meta name="keywords" content="@Model.Item.Fields["MetaDataKeywords"].Value">

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.