【问题标题】:Sitecore: base template item of a pageSitecore:页面的基本模板项
【发布时间】:2019-11-18 15:31:39
【问题描述】:

我在 Sitecore 中有一个页面项目,我知道它是从某个基本模板派生的。我有那个基本模板 ID,但我想获取这个基本模板的实例,它是我页面的一部分。我想要它作为一个项目,例如我可以获取页面内部模板的 ID。

为了更清楚:

  • 称为模板 A 的 A1 的页面
  • 模板 A 以模板 B 作为基本模板
  • B 是页面 A1 的一部分,作为称为 B1 的实例(特定于页面 A1 的字段值)

如何获得 B1 作为独立项目/ID?
任何帮助表示赞赏。
谢谢。

【问题讨论】:

    标签: asp.net asp.net-mvc sitecore sitecore8.1


    【解决方案1】:

    不太清楚你的意思?为什么要获取继承模板的实例?该项目将通过 item.fields[templateBFieldId].Value 获得您继承的所有字段

    您可以执行检查当前项目是否从该模板继承之类的操作,例如:

    public static bool InheritsFrom(this Item item, ID templateId)
        {
            return item.Template.DoesTemplateInheritFrom(templateId);
        }
    
        public static bool DoesTemplateInheritFrom(this TemplateItem template, ID templateId)
        {
            if (template == null || templateId.IsNull)
                return false;
            if (!(template.ID == templateId))
                return template.DoesTemplateInheritFrom(TemplateManager.GetTemplate(templateId, template.Database));
            return true;
        }
    
    private static bool DoesTemplateInheritFrom(this TemplateItem template, Template baseTemplate)
            {
                if (baseTemplate == null)
                    return false;
    
                return TemplateManager.GetTemplate(template.ID, template.Database).DescendsFromOrEquals(baseTemplate.ID);
            }
    

    【讨论】:

    • 感谢@nologo 的回复。这段代码很有用,我有一个方法“IsDerived”来做到这一点。我将拥有模板的实例 ID,因为我想在其上添加一个 EditFrame 按钮。我想添加这个而不是它所属的项目以避免同义词字段并从体验编辑器编辑它
    【解决方案2】:

    我对 ''yml.页面项中没有基本模板的实例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多