候补的命名规则(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不是默认已经提供了这些候补吗?是的,这只不过是一个例子说明了代码实现添加候补的方法,你当然可以定制自己特有的逻辑来实现自己所需的候补。
参考文档
==========================================
作者:二十四画生
转载请注明来源于博客园——二十四画生的Blog,并保留有原文链接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
2006-08-11 DNN模块开发系列文章(4)——与模块开发相关的类