Orchard中的形状候补(Alternates)
候补的命名规则(Naming Convention for Alternates)
映射一个模板文件到一个候补(Mapping a Template File to an Alternate)
- 在模板名称中,将下划线(_)转换为点(.)或反斜杠(\)。一个反斜杠表示模板存放在一个子文件夹。
- 将形状名称中的将双下划线(__)转换为短横线(-)。
- 对于形状名称中的不同显示类型,把显示类型名称前加点(.)放在模板名称后面。如:Content-BlogPost.Summary。
Shape type Template Folder Content item Views\Items Parts Views\Parts Fields Views\Fields EditorTemplate Views\EditorTemplates\[template type folder]
(For example, an EditorTemplate for a part is located at Views\EditorTemplates\Parts.)All other Views
例如:创建一个Tags部件候补的模板,你可以添加一个模板文件到MyTheme\Views\Parts目录下。然而,因为下划线可以被转换成一个点(.)或反斜杠(\),你同样也可以创建一个模板文件加上Parts.前缀直接放在Views目录下。一个模板无论是Views\Parts\Tags.ShowTags-BlogPost.cshtml还是Views\Parts.Tags.ShowTags-BlogPost.cshtml都可以映射到一个名为Parts_Tags_ShowTags__BlogPost的形状。
如果Orchard框架不能加载候补的模板,那么它将根据形状名称加载默认的形状模板。例如:你没有创建Tags部件候补的模板,它将默认使用Views\Parts\Tags.ShowTags.cshtml模板。
Orchard框架会自动创建很多候补,可以在你的应用程序中使用。然而,你也可以创建这些形状候补的模板。Orchard会根据以下模式来创建形状候补。
- Content__[DisplayType]. (Example template: Content.Summary)
- Content__[ContentType]. (Example template: Content-BlogPost)
- Content__[Id]. (Example template: Content-42)
- Content_[DisplayType]__[ContentType]. (Example template: Content-BlogPost.Summary)
- Content_[DisplayType]__[Id]. (Example template: Content-42.Summary)
- Zone__[ZoneName]. (Example template: Zone-SideBar)
- Menu__[MenuName]. (Example template: Menu-main)
- MenuItem__[MenuName]. (Example template: MenuItem-main)
- LocalMenu__[MenuName]. (Example template: LocalMenu-main)
- LocalMenuItem__[MenuName]. (Example template: LocalMenuItem-main)
- Style__[FileName]
- Resource__[FileName]
- Widget__[ZoneName]. (Example template: Widget-SideBar)
- Widget__[ContentType]. (Example template: Widget-BlogArchive)
- [ShapeType__FieldName]. (Example template: Fields\Common.Text-Teaser)
- [ShapeType__PartName]. (Example template: Fields\Common.Text-TeaserPart)
- [ShapeType]__[ContentType]__[PartName]. (Example template: Fields\Common.Text-Blog-TeaserPart)
- [ShapeType]__[PartName]__[FieldName]. (Example template: Fields\Common.Text-TeaserPart-Teaser)
- [ShapeType]__[ContentType]__[FieldName]. (Example template: Fields\Common.Text-Blog-Teaser)
- [ShapeType]__[ContentType]__[PartName]__[FieldName]. (Example template: Fields\Common.Text-Blog-TeaserPart-Teaser)
- [ShapeType]__[Id]. (Example template: Parts\Common.Metadata-42)
- [ShapeType]__[ContentType]. (Example template: Parts\Common.Metadata-BlogPost)
Url候补(URL Alternates)
在homepage中可以有:
明确的来指定一个候补(Explicitly Designating an Alternate Template)
<Placement> <Place Parts_Tags_Edit="Content:7"/> <Match ContentType="BlogPost"> <Place Parts_Tags_ShowTags="Header:after.7;Alternate=Parts_Tags_ShowTags_BlogPost"/> </Match> <Match DisplayType="Detail"> <Place Parts_Tags_ShowTags="Header:after.7"/> </Match> <Match DisplayType="Summary"> <Place Parts_Tags_ShowTags="Header:after.7"/> </Match> </Placement>
通过代码来添加候补(Adding Alternates Through Code)

using Orchard.ContentManagement;
using Orchard.DisplayManagement.Descriptors;
namespace MyTheme.ShapeProviders
{
public class ExampleShapeProvider : IShapeTableProvider
{
private readonly IWorkContextAccessor _workContextAccessor;
public ExampleShapeProvider(IWorkContextAccessor workContextAccessor)
{
_workContextAccessor = workContextAccessor;
}
public void Discover(ShapeTableBuilder builder)
{
builder.Describe("Content")
.OnDisplaying(displaying =>
{
if (displaying.ShapeMetadata.DisplayType == "Detail")
{
ContentItem contentItem = displaying.Shape.ContentItem;
if (_workContextAccessor.GetContext().CurrentSite.HomePage.EndsWith(';' + contentItem.Id.ToString()))
{
displaying.ShapeMetadata.Alternates.Add("Content__HomePage");
}
}
});
builder.Describe("Parts_Tags_ShowTags")
.OnDisplaying(displaying =>
{
if (displaying.ShapeMetadata.DisplayType == "Summary")
{
displaying.ShapeMetadata.Alternates.Add("Tags_ShowTags_Summary");
}
});
}
}
看了上面的例子你可能会说,Orchard不是默认已经提供了这些候补吗?是的,这只不过是一个例子说明了代码实现添加候补的方法,你当然可以定制自己特有的逻辑来实现自己所需的候补。
参考文档