This project is read-only.

Layout.Title not set when assigned in shape view

Topics: Writing modules
Jul 31, 2012 at 12:44 PM
Edited Jul 31, 2012 at 6:14 PM

I have the following situation:

  • a custom theme with a Document.cshtml rendering 
  • ... and a custom Layout.cshtml file not interfering with the Title at all
  • a custom module with a PageController and an Impressum action rendering the following Impressum.cshtml from under /Views/Page:
  • a shape view (is this the correct name?) named Impressum.cshtml under /Views with this (which can then easily be overridden on other modules):
        Layout.Title = "Impressum (Layout)";
        Html.Title("Impressum (Html)");
    <h1>Impressum Header</h1>
    <h2>Second header</h2>

With this setup I get "Impressum (Html)" as my pages title - if I omit that line the title renders empty.

My question: why doesn't the Layout.Title assignment work here?

Note: There are no ContentItems or ContentParts involved.

Jul 31, 2012 at 4:36 PM

The correct way to get the title in document.cshtml is this (as used in the default document.cshtml template):

    string title = Convert.ToString(Model.Title);
    string siteName = Convert.ToString(WorkContext.CurrentSite.SiteName);

and then this:

    <title>@Html.Title(title, siteName)</title> 

By diverging from what the default template does, you're exposing yourself to this kind of difficulty.

Jul 31, 2012 at 8:02 PM

Thanks for you answer, Bertrand, but as far as I can see I'm not diverging from the default template: in version 1.4 Document.cshtml from the SafeMode theme says exactly the same:

My question was also more about the assignment of a page's title - how do I do that from within a shape? Doing what you propose with Model.Title didn't result in any title being output.

Aug 2, 2012 at 4:51 PM

The one in SafeMode is not the one you should be copying. It's the one from Core that you should be looking at.

Layout.Title will affect the title, but there are also extensions under Html, such as AddTitlePart, that help in building titles. But for all that to work, you must have the right code in your document.cshtml, which, as I said, you don't. Just trust me on that one.