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 6 posts 83 karma points
    Mar 16, 2019 @ 09:27
    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 196 posts 840 karma points c-trib
    Mar 16, 2019 @ 15:22
    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 6 posts 83 karma points
    Mar 18, 2019 @ 07:45
    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 1285 posts 4039 karma points MVP 8x c-trib
    Mar 18, 2019 @ 08:07
    Bjarne Fyrstenborg
    100

    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>
    
  • mostafa heidary 3 posts 68 karma points
    Aug 20, 2021 @ 12:10
    mostafa heidary
    0

    Thanks Also this could be used:

    @(Model.Value<string>("metaName")?? Model.Name)
    
Please Sign in or register to post replies

Write your reply to:

Draft