【问题标题】:What is the simplest way to find current item ID in the template在模板中查找当前项目 ID 的最简单方法是什么
【发布时间】:2013-01-26 09:26:03
【问题描述】:

在我的 C# 或 dreamweaver 模板中,我需要知道我在渲染什么。问题是我不确定我是否在寻找页面或组件。我可能可以使用package.GetByType(ContentType.Page),如果它是空的 - 获取组件的内容,但我觉得应该有更短的方法。

大卫的例子更短:

engine.PublishingContext.ResolvedItem.Item.Id

【问题讨论】:

    标签: tridion tridion-2011


    【解决方案1】:
    engine.PublishingContext.ResolvedItem.Item.Id
    

    您还可以检查 Publishing Context 的已解析项目,看看它是否是一个页面(如果不是,那么它是一个组件)。

    例如:

    Item currentItem;
    if (engine.PublishingContext.ResolvedItem.Item is Page)
    {
        currentItem = package.GetByName(Package.PageName);
    }
    else
    {
        currentItem = package.GetByName(Package.ComponentName);
    }
    TcmUri currentId = engine.GetObject(currentItem).Id;
    

    如果您想快捷调用 engine.GetObject(),那么您可以直接从 Item 的 XML 中获取 ID:

    String currentId = currentItem.GetAsSource().GetValue("ID");
    

    【讨论】:

    • 这比我的方法好得多。
    【解决方案2】:

    这就是我之前看到的做法:

    // Contains the call you describe in your question
    Page page = GetPage(); 
    if (page == null)
    {
        // Contains a call using package.GetByName("Component") 
        // to avoid the situation with multiple Components on the package
        Component comp = GetComponent();
        // Do component stuff
    }
    else
    {
        // Do page stuff
    }
    

    不确定你能不能更好地封装它,但我可能会被证明是错误的。

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 2017-04-17
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2015-07-08
      • 2017-08-23
      • 2021-07-17
      相关资源
      最近更新 更多