Dejan.S
Dejan.S

Reputation: 19128

Removing whitespace from title (in head)

I'm having a "wierd" issue. My output html title element creates linebreakes and whitespace.

<title>

    Title | Brand name

</title>

I use MVC 2 and got the following code to generate my title elements

Site.Master

<title><asp:ContentPlaceHolder ID="TitleContent" runat="server"></asp:ContentPlaceHolder></title>

Page (aspx)

<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
    <%= Html.SeoTitle(Model.MetaTitle.Trim(), Html.DefaultTitle())%>   
</asp:Content>

Helper

public static string SeoTitle(this HtmlHelper helper, string title, string separator, bool siteNameLast, string fallbackTitle)
        {
            var siteName = SiteName(helper);

            title = !string.IsNullOrEmpty(title) ? title : fallbackTitle;

            if (string.IsNullOrEmpty(siteName))
                return title;

            title = title.TextToEntity();
            title = siteNameLast ? string.Format("{0} {1} {2}", title, separator, siteName) : string.Format("{0} {1} {2}", siteName, separator, title);

            return title;
         }

I'm can't find anything related to why I get this result. Any ideas?

EDIT
Applying the answers will reduce one line-break, to have the output look like bellow. Can I make it on the same line?

<title>
    title | brand name
</title>

Upvotes: 2

Views: 763

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 156978

This code causes the line breaks and white space:

<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
    <%= Html.SeoTitle(Model.MetaTitle.Trim(), Html.DefaultTitle())%>   
</asp:Content>

If you strip the start and end tags of them, you still start and end with a line break.

(removed tag, still whitespace hereafter)
    <%= Html.SeoTitle(Model.MetaTitle.Trim(), Html.DefaultTitle())%>   
(removed tag, still whitespace before)

So the solution is to immediately start the inner content of the asp:Content tag without line breaks for layout.

<asp:Content ContentPlaceHolderID="TitleContent" runat="server"><%= Html.SeoTitle(Model.MetaTitle.Trim(), Html.DefaultTitle())%></asp:Content>

As far as I know, this doesn't influence how the title is actually rendered. The whitespace is trimmed off the title.

Upvotes: 3

ankhzet
ankhzet

Reputation: 2568

Remove whitespaces here:

<asp:Content ContentPlaceHolderID="TitleContent" runat="server"><%= Html.SeoTitle(Model.MetaTitle.Trim(), Html.DefaultTitle())%></asp:Content>

Upvotes: 1

Related Questions