2013-02-11 64 views
5

在我的C#或dreamweaver模板中,我需要知道我要渲染什麼。問題是我不確定我是否在尋找一個頁面或組件。我大概可以使用package.GetByType(ContentType.Page),如果它是空的 - 獲取組件的內容,但我覺得應該有一個更短的方法。大衛什麼是在模板中查找當前項目ID的最簡單方法

實例較短:

engine.PublishingContext.ResolvedItem.Item.Id 

回答

5
engine.PublishingContext.ResolvedItem.Item.Id 

您還可以檢查出版語境下的解析項目,看看它是否是一個頁(如果它不是,那麼它是一個組件)。

例如:

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()調用,那麼您可以從項目的XML直接拿到ID:

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

這比我的方法更好。 – 2013-02-11 12:31:31

3

那怎麼我見過它做前:

// 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 
} 

不知道你能真正將其封裝比這更好的,但我可以證明錯誤。

+0

你是正確與package.GetByName(「組件」) – 2013-02-11 10:07:08

相關問題