Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tridion 在模板中查找当前项目ID的最简单方法是什么_Tridion_Tridion 2011 - Fatal编程技术网

Tridion 在模板中查找当前项目ID的最简单方法是什么

Tridion 在模板中查找当前项目ID的最简单方法是什么,tridion,tridion-2011,Tridion,Tridion 2011,在我的C#或dreamweaver模板中,我需要知道我正在渲染什么。问题是,我不知道我是否在寻找一个页面或组件。我可能会使用package.GetByType(ContentType.Page),如果它是空的,则获取组件的内容,但我觉得应该有一种更短的方法 大卫的例子比较短: engine.PublishingContext.ResolvedItem.Item.Id 这就是我以前看到的情况: // Contains the call you describe in your question

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

大卫的例子比较短:

engine.PublishingContext.ResolvedItem.Item.Id

这就是我以前看到的情况:

// 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
}
我不确定你能不能把它封装得比这更好,但我可能被证明是错的。

engine.PublishingContext.ResolvedItem.Item.Id
您还可以检查发布上下文的已解析项,查看它是否是页面(如果不是,则它是一个组件)

例如:

Item当前项;
if(engine.PublishingContext.ResolvedItem为第页)
{
currentItem=package.GetByName(package.PageName);
}
其他的
{
currentItem=package.GetByName(package.ComponentName);
}
TcmUri currentId=engine.GetObject(currentItem).Id;
如果要快捷地执行engine.GetObject()调用,则可以直接从项目的XML中获取ID:

String currentId=currentItem.GetAsSource().GetValue(“ID”);

这比我的方法好得多。