If we want to execute some code for Staging but not for the Live Publish Target we need to detect the target title during Publish Time. In this article I will show how to get the Publish Target Title in VBScript, Razor and C#.
A long long time ago in a land far away….Publication Target Title in VBScript
Before we had the power of .NET we used VBScript (or XSLT) to render our beautiful content from Tridion. The VBScript rendering engine has the TcmScriptAssistant class to provide handy methods like WriteOut and also objects such as PublicationTarget and PublicationTargetId. To get the PublicationTarget Title in VBScript we typed:
[% =PublicationTarget.Title %]
Getting the Publication Target Title with a Razor TBB
Today we have the powerful Razor Meditator for rendering our .NET and Razor code since the TcmScriptAssistant is no longer available in our Compound Templates. So, how do we get the Publication Target Title?
@{ var engine = TridionHelper.Engine; var publishContext = engine.PublishingContext; var pubTargetTitle = publishContext.PublicationTarget.Title; }
Getting the Publication Target Title in a C# TBB
We might be using DWT TBBs and want access to the Publication Target Title. In this case, we could create a C# TBB, get the Publication Target Title and push it into the Package.
PublishingContext publishContext = engine.PublishingContext; pubTargetTitle = publishContext.PublicationTarget.Title; log.Info("PubTargetTitle = " + pubTargetTitle);
Summary
Using the engine object we can get more info about our publishing environment and access properties related to the processing of the item, including publishing context details. The Publication Target Title is still there and with a bit of looking it is not too hard to find. Big thanks to Alex Klock for writing the Razor Mediator and keeping up with the bug fixes and feature requests.
+1 to the compliment to Alex’s mediator–though I have to caveat it’s not an SDL product with clients, I’ve already seen one customer enthusiastically set on adopting it.
In terms of the Publication Target Title (and Type), the use case I’ve seen is rendering something different for SiteEdit or a test environment (and maybe older custom pages/extensions). How else have you used it or seen it applied?
The use cases are exactly as you describe – anytime we want to do use a different URL or resource for Test, Staging, Production, etc. I have seen this used to specify a different WebService URL depending on environment. But, it is easy to implement and the possibilities are endless,
I enjoy working with the Razor Mediator and have seen the Razor Mediator used successfully on 2 large implementations. The challenge is to use good OO practices and structure the code in a re-usable and easy to maintain way. However, I have seen increased productivity over DWT and VBScript templating once the team is comfortable with the syntax (around 1-2 weeks). We can also use C# TBBs and Compound Templating to put the heavy processing in the C# side and use Razor to output package variables. There is lots of flexibility when using the Razor Mediator and that is the challenge – to identify clear coding guidelines and regularly audit the code to make sure it follows them. Success with your Razor project and please feel free to update the wiki and report any issues in the Google Code project.