Once the Display Options have been defined in the initialisation module, each option will have an associated tag. You then need to update the Register method, in the TemplateCoordinator class, to tell the rendering engine which view template to use to render the block:
public void Register(TemplateModelCollection viewTemplateModelRegistrator) { viewTemplateModelRegistrator.Add(typeof(TeaserBlock), new TemplateModel { Name = "TeaserBlock_Quarter", Tags = new[] { "Quarter" }, AvailableWithoutTag = false, Path = BlockPath("TeaserBlock_Quarter.cshtml") }); viewTemplateModelRegistrator.Add(typeof(TeaserBlock), new TemplateModel { Name = "TeaserBlock_Half", Tags = new[] { "Half" }, AvailableWithoutTag = false, Path = BlockPath("TeaserBlock_Half.cshtml") }); }
In this scenario, you will have three different views for the TeaserBlock - named TeaserBlock.cshtml, TeaserBlock_Quarter.cshtml and TeaserBlock_Half.cshtml. When the editor adds the block to a content area, the display option set for that instance of the block will determine which view to use to render the content. (The above code is based on Alloy template).
Ok thanks.
So I need x amount of views depending on the amount of layouts I have? There is no way of getting around this since I have quite a lot of layout widths...
The selected display option is stored as a property of the content area rather than the block itself, so the only other way is to have the view call out to the parent page and query the content area items properties. This would add serious performance overheads to your pages, and could lead to page timeouts and ultimately require a restart of the environment to free up resource.
if you are using bootstrap framework for layouting your blocks, then this package might be handy for you. it has also support for `@Html.GetDisplayOption(Model)` in your block template - returning strongly typed DisplayOption object.
I followed this guide https://world.episerver.com/documentation/developer-guides/CMS/rendering/display-options/ creating display options for all blocks.
How can I get the selected display option in the block views? I need to display different images depending on what layout is selected..