SDL Tridion’s Content Manager templating API (TOM.NET) offers ways to detect publishing or rendering context.
Use Cases
- Present debugging information to a specific environment (e.g. TCM Uris only on Staging)
- Show different markup in Preview (e.g. show a link to the published page)
- Show different author-able fields in Experience Manager or SiteEdit
I’ve seen and tried a few examples, but after following a chat between colleagues Stan and Eric, I want to make sure I follow for TOM.NET (6.1 / Tridion 2011).
Scenarios
- Publishing to a specific Publication Target (typically “Live” and “Staging”)
- Content Manager Explorer (CME) Preview
- Session Preview rendering for Experience Manager (XPM)
- (Added) Template Builder
1. Publishing to a Target (or from a Publication)
Tridion.ContentManager.Publishing.PublishEngine.GetPublishInfo(IdentifiableObject item)
Item would be a page or component. This returns a collection of PublishInfo objects, which includes PublicationTarget to confirm where you’re publishing to.
Tridion.ContentManager.Templating.PublishingContext.PublicationTarget has PublicationTarget as well.
2. CME Preview
PublicationTarget is null, which makes sense because you don’t have a Publication Target . 🙂
3. Session Preview
Use the RenderMode Enum under Tridion.ContentManager.Publishing, which has:
- ‘Publish’ (0)
- ‘PreviewStatic’ (1)
- ‘PreviewDynamic’ (2)
PublicationTarget wouldn’t be null for Session Preview, which isn’t really publishing.
4. (Added) Template Builder
?
Alexander Klock also describes some related examples which cover most of this except CME Preview.
Question(s)
-
Am I missing any scenarios? Publish to a specific publication target, regular preview, and XPM session preview?
-
How should I avoid hard-coding PublicationTargets (e.g. is it better to check the string values instead of TCM Uris)?
-
Update: added Template Builder to the list per Vikas’s answer, how do I know I’m rendering within Template Builder?
You really need a tl;dr on this question…
Here’s what I know:
Template Builder
Publication target is null, RenderMode is PreviewDynamic
CME Preview
Publication Target ID is tcm:0-0-0 (or
TcmUri.UriNull), RenderMode is PreviewDynamicSession Preview
Publication Target ID is the real target ID, RenderMode is PreviewDynamic
Publish
Publication Target ID is the real one, RenderMode is Publish
EDIT
Here’s some sample code I wrote recently to determine the Current mode.