generating page title with razor script - umbraco

1.4k views Asked by At

So I am trying to create a script whereby depending on the document type of the page a certain pre-defined title tag format will appear, if there is nothing already written in an overwriting custom title input. I have inserted the macro within the title tag on my master template but keep on getting an Error loading Razor Script message .

Html

<title>
    <umbraco:Macro Alias="NewPageTitle" runat="server"></umbraco:Macro>
</title> 

Script -

@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines

    @{
      if(String.IsNullOrEmpty(@Model.tabName.ToString()) == false )
        {
          @Model.tabName
        }

      else if(@Model.DescendantsOrSelf("Country"))
        {
          <text>
            Holidays in @Model.Name
          </text>
        }

      else 
        {
          @Model.Name;
        }
    }

Any help would be greatly appreciated.

2

There are 2 answers

2
Hideous1 On

Try this code out. The problem with your original code is that you were using "@Model.DescendantsOrSelf("Country")" as a boolean, and it is a list. I also removed your comparison for if(String.IsNullOrEmpty(@Model.tabName.ToString())).

Also, if you add ?umbDebugShowTrace=true to the end of your URL, you can get some valuable debugging information. There is a Chrome Extension called "Umbraco Debug" that I use to quickly access that query string and information. You may find it useful.

@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines

    @{
      if(String.IsNullOrEmpty(@Model.tabName.ToString()))
        {
          @Model.tabName
        }

      else if(@Model.DescendantsOrSelf("Country").Count() > 0)
        {
          <text>
            Holidays in @Model.Name
          </text>
        }

      else 
        {
          @Model.Name;
        }
    }
0
Nimesh khatri On

its very simple just add following code into your title tag

@Umbraco.Field("pageName")

will display pageName,you may also add custom properties from document type. e.g. you have added new property like "metaKeywords" with value "html,javascript,xml",fetch that values as following way...

@Umbraco.Field("metaKeywords")

even you don't need to add custom properties in your model