在 EpiServer 中渲染不同的部分模板
Render different partial templates in EpiServer
我有一个页面部分,当页面添加到 ContentArea 时,它应该在 ContentArea 内呈现。这非常有效,但现在我在两个不同的页面上有两个不同的 ContentAreas,我希望将相同的子页面添加到这些页面以在每个父页面上呈现不同的内容。
我知道我可以在渲染局部时以某种方式使用标签来区分 ContentAreas:
@Html.PropertyFor(m => m.MyBlockProperty, new { Tag = RenderingTags.Sidebar })
@Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar })
但是,在我的 SomePage.cshtml(这是我的局部视图)中,我是否在这里得到了一个变量或其他东西,所以我知道请求了哪个标签?或者是否有像 SidebarSomePage.cshtml 这样的命名约定,以便我可以定义多个部分模板?我是否必须创建一个控制器来处理这个问题?对我来说似乎没有必要,我只是想根据页面稍微更改 html...
我很确定您可以像这样从视图(或控制器)中的 ViewData 字典访问标签:
@ViewData["Tag"]
您还可以将任何其他设置传递给视图
@Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar, RenderThisPartialDifferently = true, ShowHeading = false })
然后访问它们:
@ViewData["RenderThisPartialDifferently"]
@ViewData["ShowHeading "]
然后您可以选择在两者之间放置一个控制器并呈现完全不同的视图。
很确定标签视图也有命名约定。不过,我确实知道的是,您可以在 /shared/displaytemplates 中放置一个与标签同名的视图。但这不是你现在要求的。
创建一个 PartialContentController<T>
,然后使用 TemplateDescriptorAttribute
指定您要使用的标签。然后按照 Jonah 在视图中解释的那样使用 Property For。
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.
此外,除了所有答案之外,您还可以使用模板注册器为特定标签注册其他模板。
[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")
});
}
}
这将确保如果块被呈现 "inside" RenderingTags.Sidebar
上下文(例如通过 Html.PropertyFor(...., new { tag = RenderingTags.Sidebar }))
文件 Some-Other-Template.cshtml
将被使用。
AlloyTech 那里有示例代码。
我有一个页面部分,当页面添加到 ContentArea 时,它应该在 ContentArea 内呈现。这非常有效,但现在我在两个不同的页面上有两个不同的 ContentAreas,我希望将相同的子页面添加到这些页面以在每个父页面上呈现不同的内容。
我知道我可以在渲染局部时以某种方式使用标签来区分 ContentAreas:
@Html.PropertyFor(m => m.MyBlockProperty, new { Tag = RenderingTags.Sidebar })
@Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar })
但是,在我的 SomePage.cshtml(这是我的局部视图)中,我是否在这里得到了一个变量或其他东西,所以我知道请求了哪个标签?或者是否有像 SidebarSomePage.cshtml 这样的命名约定,以便我可以定义多个部分模板?我是否必须创建一个控制器来处理这个问题?对我来说似乎没有必要,我只是想根据页面稍微更改 html...
我很确定您可以像这样从视图(或控制器)中的 ViewData 字典访问标签:
@ViewData["Tag"]
您还可以将任何其他设置传递给视图
@Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar, RenderThisPartialDifferently = true, ShowHeading = false })
然后访问它们:
@ViewData["RenderThisPartialDifferently"]
@ViewData["ShowHeading "]
然后您可以选择在两者之间放置一个控制器并呈现完全不同的视图。
很确定标签视图也有命名约定。不过,我确实知道的是,您可以在 /shared/displaytemplates 中放置一个与标签同名的视图。但这不是你现在要求的。
创建一个 PartialContentController<T>
,然后使用 TemplateDescriptorAttribute
指定您要使用的标签。然后按照 Jonah 在视图中解释的那样使用 Property For。
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.
此外,除了所有答案之外,您还可以使用模板注册器为特定标签注册其他模板。
[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")
});
}
}
这将确保如果块被呈现 "inside" RenderingTags.Sidebar
上下文(例如通过 Html.PropertyFor(...., new { tag = RenderingTags.Sidebar }))
文件 Some-Other-Template.cshtml
将被使用。
AlloyTech 那里有示例代码。