Search code examples
razorepiserver

Render different partial templates in EpiServer


I have a page partial that is supposed to render inside a ContentArea when the page is added there. This works perfectly, but now I have two different ContentAreas on two different pages and I want the same child page added to those to render different on each parent page.

I get that I could in some way use a Tag when rendering the partial to differentiate between the ContentAreas:

@Html.PropertyFor(m => m.MyBlockProperty, new { Tag = RenderingTags.Sidebar })
@Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar })

But then, in my SomePage.cshtml (which is my partial view), do I get a varaible or something here so I know which Tag was asked for? Or is there some naming convention like SidebarSomePage.cshtml so that I can define multiple partial templates? Do I have to create a controller to deal with this? It seems unneccessary to me, I just want to change the html a bit depending on page...


Solution

  • I'm pretty sure you can access the tag from the ViewData dictionary in your view (or controller) like this:

    @ViewData["Tag"]
    

    You can also pass any other setting to the view

    @Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar, RenderThisPartialDifferently = true, ShowHeading = false })
    

    And then access them:

    @ViewData["RenderThisPartialDifferently"]
    @ViewData["ShowHeading "]
    

    And then you have the option to have a controller in between and render a completely different view.

    Pretty sure there is a naming convention for tag views as well. What I do know for sure though, is that you can put a view with the same name as the tag in /shared/displaytemplates. But that's not what you're asking for now.