SaaS CMS has officially launched! Learn more now.

Rendering multiple templates for blocks without controller

Vote:
 

Hi All,

 I have requirement to render multiple templates for block with having only partial views(no controller).Currently we are using a common page for rendering some blocks.As template descriptors won't work I used IViewTemplateModelRegistrator to register multiple templates for my block.Now the problem is I can't use tag for template resolving as I'm using common page for rendering blocks.Is there any other way for template resolving.

Can someone help me in solving this issue.Please refer to the code below.

Common page to render multiple blocks:

public class OneColumnPage : HeaderBannerBase
{
/// <summary>
/// Gets or sets the main body
/// </summary>
[CultureSpecific]
[Display(
Name = "Main body",
GroupName = SystemTabNames.Content,
Order = 1)]
[AllowedTypes(new Type[]
{
typeof(TextBlock), typeof(ProductBlock), typeof(ProductTrainingBlock), typeof(PlaceBlock), typeof(TeamInfoBlock), typeof(AboutUsKeynoteBlock), typeof(AboutUsBannerBlock), typeof(AboutUsBlock), typeof(ImageGalleryBlock), typeof(GridBlock), typeof(ClaimsFormsDownloadBlock),
typeof(DocumentBlock)
})]
public virtual ContentArea MainBody { get; set; }

cshtml

@model PageViewModel<OneColumnPage>

@Html.PropertyFor(p => p.CurrentPage.MainBody, new { Tag = "_CustomContentArea" })

Code in Template Coordinator:

viewTemplateModelRegistrator.Add(
typeof(DocumentBlock),
new TemplateModel() { Name = "claimForms", Description = "Displays as claimForms.", Path = "~/Views/Shared/Blocks/DocumentBlock2.cshtml", Tags = new string[] { "claimForms" }});

#203902
May 10, 2019 10:11
Vote:
 

Hi Likhitha,

I'm not quite sure of how you'd like to decide which template to use but it sounds like you could use display options to allow your editors to choose the template used to render a given block. There's an example of how to do that in the Alloy site using page partials but the same applies to blocks.

#203909
May 10, 2019 13:13
Vote:
 

Hi Paul,

 This requirement is not about allowing template selection from CMS edit.It's for developers to render different template based on UI structure so i can't go for display options.

#203927
May 11, 2019 17:20
Vote:
 

Hi Likhitha,

What do you mean by UI structure?

You could programatically set the tag used for rendering the content area if a single tag will suffice for all items in the content area. If you need more granular control over how the blocks are rendered within the content area you could create your own ContentAreaRenderer and override RenderContentAreaItem() (and possibly GetContentAreaItemCssClass()) to add in your own logic to choose your tag. Alternatively, you could simply loop through the filtered items in the content area in your view and render each item using Html.RenderContentData, passing in the tag you want to use to render the item (though be careful not to break on-page editing).

#203930
May 12, 2019 20:50
Vote:
 

Thank you Paul.WIll try.

#203935
May 13, 2019 9:26
* 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.