1
votes

I have a couple different types of pages that need to be styled differently but would pull info from the same fields.

Using webforms with Sitecore, I would probably make these two different sublayouts. Either one could be applied to the same item type.

I'm looking for something that would be the same for Sitecore MVC.

I could make two separate templates and point them to different entry points on a controller or make a field that determines which of the two styles to use.

I was wondering if there's another option I don't know about that somehow puts nested partial views in the hands of content editors in the way that layouts/sublayouts do?

2
You should use viewrendering or controllerendering. In your view you use placeholder like derek answerThinhLe

2 Answers

4
votes

This sort of requirement can usually be handled by having multiple View Renderings that share the same Model. When using a View Rendering, Sitecore itself becomes the controller, and is responsible for combining a Model to a Razor view (.cshtml). You can then use the View Renderings just as you would Sub Layouts and apply them to a Template's Standard Values, or allow them to be inserted into placeholders.

A View Rendering is not required to have a Model, and it is quite common to have a View Rendering that merely contains some scaffolding HTML and a nested placeholder. Example:

<div class="container">
    @Html.Sitecore().Placeholder("my-nested-placeholder")
</div>

NOTE: If more advanced logic is needed (such as custom routing, query string parsing, etc., then you can just as well use multiple Controller Renderings).


Another approach I sometimes take for altering styles is to apply a "body class" to my pages via a Template's standard values. The body class field can be a simple Single-Line text field that is hidden to novice users. You can then inject CSS classes onto your different page types and alter and/or target elements in your markup using CSS inheritance.

3
votes

Equivalents to sublayouts in Sitecore MVC are View Renderings and Controller Renderings (as Derek already said). I would also suggest to create two separate View Renderings and share the same model.

Another approach to choose which rendering to use if conditional rendering. If you have the same item request and need a different view for several conditions, this may be worth a try. A good starting point for this is the blog post from John West and the Rules Engine Cookbook.