November Happy Hour will be moved to Thursday December 5th.

Get block current display option in block view

Vote:
 

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..

#197620
Oct 09, 2018 8:48
Vote:
 

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).

#197623
Oct 09, 2018 9:12
Vote:
 

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...

#197638
Oct 09, 2018 9:51
Vote:
 

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.

#197640
Oct 09, 2018 10:11
Vote:
 

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.

#197642
Oct 09, 2018 12:40
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.