Question

In my C# or dreamweaver template I need to know what am I rendering. The problem is that I don't know for sure if I'm looking for a page or component. I could probably use package.GetByType(ContentType.Page) and if it's empty - get content of a component, but I feel there should be a shorter way.

Example of David is shorter:

engine.PublishingContext.ResolvedItem.Item.Id
Was it helpful?

Solution

engine.PublishingContext.ResolvedItem.Item.Id

You can also check the Publishing Context's resolved Item and see if it's a Page or not (if it's not, then it's a Component).

For example:

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;

If you want to shortcut the engine.GetObject() call, then you may be able to get the ID from the Item's XML directly:

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

OTHER TIPS

That's how I've seen it done before:

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

Not sure you can encapsulate it much nicer than that really but I may be proved wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top