Using Placement.info to specify alternates

A little used feature of Orchard's Placement.info is the ability to define custom alternates to be used within it.

A previous developer at my company needed a part to be formatted slightly differently based on the DisplayType. To this end he wrote a class that generated alternates for parts based on the current DisplayType. This may have seemed like a good idea at the time but in practise, it generates a huge number of useless alternates that will only be of use in one scenario, which does not seem particularly efficient. The simpler solution would have been to use the placement file.

Within your <place> tags you can add extra arguments after the standard location and priority. So let's take a look at what this would look like.

<Match ContentType="Page">
  <Place Parts_Title="Header:1"/>
  <Match DisplayType="Detail">
    <Place Parts_Title="/PageTitle:3;Alternate=Parts_Title__Detail"/>
  </Match>
</Match>

So as you can see, when we match a content type of "Page" and a display type of "Detail", we will offer an alternate of Parts_Title__Detail, which we can create in our theme, either manually or via Shape Tracing.

Shape Tracing Image

As you can see, the alternates corresponding file name is Parts.Title-Detail.cshtml, one underscore (_) becomes a dot (.) and two underscores (__) becomes a dash (-). Another interesting thing to note is the use of the forward slash (/) in front of PageTitle in this line:

 <Place Parts_Title="/PageTitle:3;Alternate=Parts_Title__Detail"/>

PageTitle is a custom zone I have defined in my Layout.cshtml (and added to the list of zones in the Module.txt!)

    @if (Model.PageTitle != null) {
        <header id="page-title">
            <div class="container">
                @Zone(Model.PageTitle)
            </div>
        </header>
    }

So using the forward slash (/) pushes parts into Layout Zones, not in Content Zones, which can come in pretty handy.