4

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

  1. Publishing to a specific Publication Target (typically "Live" and "Staging")
  2. Content Manager Explorer (CME) Preview
  3. Session Preview rendering for Experience Manager (XPM)
  4. (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?

0

2 Answers 2

4

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 PreviewDynamic

Session 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.

private CurrentMode GetCurrentMode()
{
    RenderMode renderMode = _engine.RenderMode;
    if (renderMode == RenderMode.Publish) return CurrentMode.Publish;


    if (renderMode == RenderMode.PreviewDynamic)
    {
        if (_engine.PublishingContext.PublicationTarget == null) return CurrentMode.TemplateBuilder;
        PublicationTarget target = _engine.PublishingContext.PublicationTarget;
        if (target.Id.Equals(TcmUri.UriNull)) return CurrentMode.CmePreview;
        return CurrentMode.SessionPreview;
    }
    return CurrentMode.Unknown;
}

private enum CurrentMode
{
    TemplateBuilder,
    CmePreview,
    SessionPreview,
    Publish,
    Unknown
}
1
  • And risk getting a downvote? I should just spawn off a blog post. :-p +1 to Vikas and your answer. Accepted for answering on Template Builder. Feb 1, 2013 at 23:33
4

You presented a very good view of complete publishing/preview model. Here are my thoughts..

Are we missing any scenarios? 

I think you covered everything expect the template builder case, which is similar to CME preview where we get publishing target as null but can be used to check different condition so important for debugging purpose.

How should I avoid hard-coding PublicationTargets  

Yes we should never use tcm uri in any code, as you suggested we could use name and even name can be configured in respective config files for that program.

Also may not be relevant here, its always good to have separate target for Tridion UI edting other than staging. Both can be configured on same server with two deployer's. One could be staging.yoursite.com and other could be tridionui.yoursite.com

Thanks..

3
  • Good catch, what else can we check to confirm if we're running in Template Builder? I noticed @@ID@@ gives me a component ID in Template Builder, but a template ID in other contexts. Probably not the best way to check, though. Jan 29, 2013 at 1:19
  • one good thing about template builder is you don't need to checked-in to see changes, you can open template, tbb or any other context item and see different result on save only. Jan 29, 2013 at 7:38
  • True, it runs on minor versions of templates at least in 2011. Save and run. But how do I check I'm running in Template Builder? Feb 1, 2013 at 1:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.