HTML Tags in <Meta Name="Title" Content="x" />

Topics: Customizing Orchard
Jan 7, 2015 at 5:51 PM
We have a handler that renders our Title value into the <Head> section of our page. I need a way to strip out <i>'s and <em>'s when they occur - as they show up without encoding in search engines. Is there an Orchard way to handle this? Otherwise, I will have to write a helper of some kind - which I may struggle with. Here's what the code in our Handler looks like:
        private void RegisterTitleMeta(BuildDisplayContext context, TitlePart part) {
            if (context.DisplayType != "Detail")
                return;

            if (String.IsNullOrWhiteSpace(part.Title))
                return;

            _resourceManager.SetMeta(new MetaEntry {
                Name = "Title",
                Content = part.Title
            });
        }
Any friendly advice appreciated. Thanks!
Toby
Jan 7, 2015 at 8:53 PM
Edited Jan 7, 2015 at 9:02 PM
In Orchard, there are some string extensions to do that, take a look at Orchard/Utility/Extensions/StringExtensions.cs

You have, particularly, this one
    public static string RemoveTags(this string html, bool htmlDecode = false)
Usage
    using Orchard.Utility.Extensions;
    ...
    part.Title.RemoveTags();
Regards
Jan 7, 2015 at 9:42 PM
Thanks but shoot it didn't work. I added the using and the extension as you suggested.

This is what I see in source HTML.
<meta content="Some text &lt;i>Text In Italics&lt;/i>" name="Title" />
Weird!
Jan 7, 2015 at 9:47 PM
Edited Jan 7, 2015 at 10:07 PM
Because of the html entity
"&lt;"
That represent this char "<". It seems that your Title is partially already Html encoded. So, try
HttpUtility.HtmlDecode(yourTitle).RemoveTag();
If it doesn't work, I will try it

Regards
Jan 8, 2015 at 1:58 AM
First, the convention for the Title is to not use a meta tag. There is a special <Title> tag for that, see in Document.cshtml
    <title>@Html.Title(title, siteName)</title> 
I've tried your method RegisterTitleMeta() directly in the TitlePartHandler by declaring
    OnGetDisplayShape<TitlePart>(RegisterTitleMeta);
For testing, if I use a test variable variable that I init to "Some <i>text</i>", you're right, the result is
    "Some &lt;i>text&lt;/i>"
But, if before _resourceManager.SetMeta() I do the following, it's ok
    var title = "Some <i>text</i>";
    title = title.RemoveTags();
Use an intermediate variable, don't assign the part.Title to not write to the database
Finally, I don't undertand how your part.Title can have some html tags

Note: I've a feature that update a Description meta tag and a Title tag. If you need help, I can give you the code

Regards
Jan 8, 2015 at 8:52 PM
Hey Thanks! I got there. We use the Google Search Appliance for our site and require the Title to display twice in our <HEAD>. Once as <title> and again as <meta>.

This code in our handler works great!
        using Orchard.Utility.Extensions;

        private void RegisterTitleMeta(BuildDisplayContext context, TitlePart part) {
            if (context.DisplayType != "Detail")
                return;

            if (String.IsNullOrWhiteSpace(part.Title))
                return;

            _resourceManager.SetMeta(new MetaEntry {
                Name = "Title",
                Content = part.Title.RemoveTags(true)
            });
        }
And in Document.cshtml
     @using Orchard.Utility.Extensions;
    
     <title>@Html.Title(title.RemoveTags(true), siteName)</title>
Thanks again for taking the time to respond. Hopefully this thread will help others as well.
Cheers,
Toby