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


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.


Create a PartialContentController<T> and then use the TemplateDescriptorAttribute to specify the tags you wan't to use. Then use PropertyFor as Johan explained in the view.

From the EPiServer documentation

The template you choose to render a content instance depends on the specific context such as channel and tagging. For a template to be automatically registered it has to implement EPiServer.Web.IRenderTemplate (where T states which model it can render). If you use a base class for your template like PageBase, ContentControlBase, BlockControlBase, PageController, PartialContentController or BlockController, then you do not need to explicitly implement the interface because that is done by the base class. In addition, you can use the TemplateDescriptorAttribute to specify more details about the template such as tags and inheritance, more information on that topic later.


Also addition to all answers, you can use template registrator to register additional templates for specific tags.

[ServiceConfiguration(typeof(IViewTemplateModelRegistrator))]
public class TemplateCoordinator : IViewTemplateModelRegistrator
{

    public void Register(TemplateModelCollection viewTemplateModelRegistrator)
    {
        viewTemplateModelRegistrator.Add(typeof(MyBlock), new TemplateModel
        {
            Tags = new[] { RenderingTags.Sidebar },
            AvailableWithoutTag = false,
            Path = BlockPath("Some-Other-Template.cshtml")
        });
    }
}

This will make sure that if block is rendered "inside" RenderingTags.Sidebar context (for instance via Html.PropertyFor(...., new { tag = RenderingTags.Sidebar })) file Some-Other-Template.cshtml will be used.

AlloyTech has sample code there.

链接地址: http://www.djcxy.com/p/68878.html

上一篇: 链接到另一个episerver页面上的定位点

下一篇: 在EpiServer中渲染不同的部分模板