Layout.cshtml and Page Content Types

Topics: Customizing Orchard, Writing themes
Aug 21, 2011 at 4:55 PM

I'm gradually peeling the onion that is Orchard and fighting my CMS assumptions based on my Wordpress experience. I've run into a problem that seems like it should have a fairly straightforward answer, but every bit of information out there I find just touches on what I'm trying to do and never deals with it directly.

I want to use a custom layout for any Pages on my site (that is, anything Page Content Type). I have a custom theme, and have customized my Layout.cshtml template. Currently, a Page is rendered using the Layout.cshtml template, and I'm thinking there has to be a way to specifically set a certain template to a particular content type. I also want to do this via my own custom code, not as the result of a gallery Module that allows me to glue things together via the admin page. I'm expecting I can do the same for other custom content types as well, so if I create a Content Type called "Race" there should be a way to say that all Race content items will use the "Layout-Race.cshtml" template or something along those lines.

My expectations may be totally incorrect, but this seems like it should be a common problem that has a straightforward solution. Can anyone provide some assistance?

Aug 21, 2011 at 5:32 PM

Actually I am not sure you really need a different Layout per content type. It might be but let me first suggest you some already existing solution.

Generally you would like to change a Layout for colums, header locations for instance. If you want something base on the content, it’s usually to organize the content itself is different sections.

You can already do it with the Content-CONTENTTYPE shape. Create new local zones inside, and organize what part is displayed where using the

The Layout is usually changed for the homepage, and it’s already handled with Url Alternate module, as Layout-url-homepage. And it works for any url too.

If you really need a Layout per content type, then there is a solution, it just imply a little bit of code. I can explain you.

Aug 21, 2011 at 5:43 PM

A bit more reading and your response have helped clear up my confusion quite a bit. Let me present a hypothetical scenario and we'll see if what I'm thinking makes sense.

Let's assume my site is going to have three different content types: Pages, Blog Posts, and a custom type called Races. I want each content type to render in a unique way. I want the homepage to render in a unique way.

In my Layout.cshtml, I'll have a @Display(Model.Content) that will render a content item. So then that would mean, to achieve my goals of having a unique home page, a unique Page layout, a unique Blog Post layout, and a unique Race layout I would need to do the following:

  • For the content item on my homepage, I could control the layout with a Content-HomePage.cshtml file, which will also contain a @Display(Model.Content) if I need to embed a particular content item on the homepage in a specifically rendered manner.
  • For a Page content item (let's say it's I would have a Content-Page.cshtml file defining how this Page content type is rendered, and it would contain @Display(Model.Title), @Display(Model.Description) or whatever to get those parts for my specific page.
  • For a Blog Post content item (let's say it's I would have a Content-BlogPost.cshtml file defining how a BlogPost content type is rendered (assuming it should be rendered differently from other content types).
  • For a Race content item (let's say it's I would have a Content-Race.cshtml file defining how a Race cotnent type is rendered (again, assuming it should be rednered differently from other content types).

Am I starting to grok this a bit better?

Aug 21, 2011 at 9:52 PM

So you don't need to change the page Layout, but the Content layout, which you can already do by default. You have to think of Pge Layout as the Master Page notion in ASP.NET, whereas Content Layout is an inner organization of content inside the Layout's Content Zone.

The main Layout defines global Zones, which might even be empty and not rendered. That why the default theme's layout comes with foldable zones.

So for you example, create those content types and some content, then enable the Shape Tracing module, and look at the available alternates, and you might even create thes alternates named Content-BlogPost, Content-Page, Content-Race, and change their content just to visualize what is possible to be done.

From those templates, you can even render specific content to the main Layout's zones, by doing something like Layout.AsideSecond.Add("foo", "after").