Friday, February 12, 2010

How Sitecore Determines Which Layout To Use

Recently I've written about the core components of Sitecore's approach to presentation logic: layouts and controls (.NET and XSLT) and placeholders. I've explained how content is displayed in layouts, and how controls are positioned inside them. What I haven't covered is how Sitecore knows which layout to use and when. That is the topic of this post.

How content is viewed in Sitecore?
When someone wants to view content from Sitecore, a request is made through a URL. In Sitecore, every URL resolves to an item in the system. What Sitecore returns when a request is made is a visual representation of the item that corresponds to the URL. We know that layouts provide the basic structure of this visual representation, but how does Sitecore know which layout to apply to which item?

What is a device?
A device represents a specific way of viewing content in Sitecore. Sitecore analyzes a request in order to determine how the user wants to view content.

This determination is based on rules a developer configures. Sitecore allows the browser agent and query string values to be used to define rules. (Of course, it is also possible to extend Sitecore to support other criteria.) If you anticipate visitors will use desktop web browsers and mobile phones to access your site, you might configure 2 devices.

Device configuration options.

It's important to note that devices themselves don't do anything. A device does not specify which layout should be used. Sitecore inspects incoming requests in order to determine which device is appropriate for each request. After Sitecore figures this out, the device is just one of many pieces of information that are passed along to be used by other parts of the system. Other parts of the system may contain conditional logic that considers the device as a part of its operation.

I think the word device fails to convey the flexibility of devices. A visitor to the website can start out using one device and then can switch to another device without leaving his browser. Devices could be used to try out new HTML using live content without disrupting the live site. I'm not sure "device" is the best English word for the concept, but that is the word that Sitecore uses.

How are devices configured?
Devices are configured under the /sitecore/Layout/Devices node. After you create a device, you can specify the browser agent value and/or the query string value that Sitecore should use to uniquely identify the device. These values can be changed at any time in the future, so you don't need to be too worried about making a mistake.

BTW, when you provide a browser agent value, Sitecore checks to see if your value is found in the User-Agent request header. Sitecore performs a case-insensitive search using IndexOf().

How does a device relate to an item?
When you want to view an item, you always view the item through a device. Therefore it makes sense that device settings can be configured at the item level. However, since all items that are based on the same data template are probably displayed in the same way, a better place to configure device settings is on the data template itself (using standard fields, of course).

By configuring the device settings on the data template you are setting a default value. You're telling Sitecore that unless the device settings are overridden on a specific item, these are the settings Sitecore should use. One of the device settings indicates which layout Sitecore should use.

Presentation Details
The section of the Sitecore user interface that allows a layout to be specified for a device is called "presentation details". Like I mentioned above, presentation details can be set on data templates and on individual items (though the former is preferred).

Presentation details in the Sitecore user interface.

The presentation details section displays all of the devices that have been defined. For each device, they layout that has been assigned for the device is displayed. This is tell you that when a certain item is viewed using a certain device, a certain layout should be used. These settings can be changed at any time.

The layout details screen lists devices and the layout assigned to each.

What's next?
Now that I've gotten through the absolute basics of content modeling and presentation logic using Sitecore, I'm going to move onto different topics. Hopefully some that won't take almost 10 posts to cover.

Want to learn more?

1 comment:

  1. Great post!

    I just want to comment about presentation layout details settings in Sitecore.

    You suggest that these settings are often defined on data template items themselves.

    With regard to backward compatibility, this is possible, but a current recommended practice is to instead define presentation layout details settings on a data template’s__Standard Values item.

    Using the Template Manager, you can create the Template Standard Values item. First, select a data template. Make sure that you see the Builder Editor tab which allows you to easily create Field Sections and Fields. In the Ribbon, select the Build Options tab. In the Template group, select the Standard Values command. In the Content Tree, you’ll see the newly created __Standard Values item. In the Editor, you’ll see the __Standard Values item itself. Now, apply your layout details on this item.

    The benefit of applying presentation layout details settings on the __Standard Values item is that it is not only inherited by all items of this type, but it is also possible to inherit these settings using base template inheritance.

    There are plenty of more reasons to use always create Template Standard Values, such as default field values (static and dynamic) and other default settings (workflow and insert options).

    ReplyDelete

Note: Only a member of this blog may post a comment.