Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Brent Welke 5 posts 72 karma points
    6 days ago
    Brent Welke
    0

    How to get the current page's title to set the <title> tag in the Master Template?

    In version 7.x this is what I would use in the Master Template to set the browser page

  • Tarik 42 posts 240 karma points
    6 days ago
    Tarik
    0

    Brent Welke, peace be upon those who follow guidance.

    I suggest to create a common document type property i.e title for all pages and add the following code in masterpage:

    <title>@Umbraco.Field("title")</title>
    
  • Brent Welke 5 posts 72 karma points
    4 days ago
    Brent Welke
    0

    Hi Tarik,

    Thanks for the quick reply. That didn't work for me, it produced an error.

    I am using the CodeShare Starter Kit v2.0 package and eventually I found the page title was being set in a partial view "/SiteLayout/_MetaData" and I just needed to append my website name to it.

    :)

  • Bjarne Fyrstenborg 1116 posts 2994 karma points MVP 2x c-trib
    4 days ago
    Bjarne Fyrstenborg
    0

    Note that the method .Field() has been removed in Umbraco v8, so it won't work.

    The CodeShare Starter Kit v2.0 package use metaName property on the current node, which might come from a property directly on the document type or inherited from a composition document types, e.g. SEO or similar.

    https://github.com/prjseal/CodeShare-Umbraco-Starter-Kit-for-v8/blob/master/src/CSUSK.Web/Views/Partials/SiteLayout/_MetaData.cshtml#L5

    You could also do something like this, which fallback to name of node in case metaName property is null, but it think this doesn't use default value if metaName just contains whitespace.

    <title>@(Model.Value<string>("metaName", defaultValue: Model.Name))</title>
    

    You could also do the following:

    var metaName = Model.Value<string>("metaName");
    <title>@(!string.IsNullOrWhiteSpace(metaName) ? metaName : Model.Name)</title>
    
Please Sign in or register to post replies

Write your reply to:

Draft