Umbraco 8 Metadata Snippet, keywords, meta title.

Umbraco 8 Metadata Snippet, keywords, meta title, meta description:

The following is a snippet that may help with generating metadata. I created this as a partial and added it to the Main Template.

If the metadata fields are not filled in on the backend by the editor, they are generated from the Page Name and the Name of the Website (applicationName).

----

In the example the backend meta fields are called:
"metaTitle"   (textstring)
"metaDescription"   (textstring)
"metaKeywords" (tags)

----

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
    var applicationName = "My Website Name";
}
@if(Model.HasValue("metaTitle")){
    <title>@Model.Value("metaTitle")</title>

else
{
    if(Model.Id == Umbraco.ContentAtRoot().FirstOrDefault().Id)
    {
        <title>@applicationName</title>
    }
    else
    {
        <title>@Model.Name | @applicationName</title>
    }
}


@if(Model.HasValue("metaDescription")){
    <meta name="description" content="@Model.Value("metaDescription")">
}
else{
    <meta name="description" content="The @Model.Name page of the @applicationName website.">
}


@if(Model.HasValue("metaKeywords")){
    var keywords = "";
    foreach(var tag in Model.GetProperty("metaKeywords").Value<IEnumerable<string>>()){
        keywords += @tag + ", ";
    }
    keywords = keywords.Trim(", ");
    if(keywords != "")
    {
        <meta name="keywords" content="@keywords">
    }
    else
    {
        <meta name="keywords" content="@applicationName, @Model.Name">
    }
}
else
{
    <meta name="keywords" content="@applicationName, @Model.Name">
}


Comments

Popular posts from this blog

Umbraco Razor Sort Nodes Ascending or Descending

Umbraco Razor get Querystring

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco