Previously I have made use of Context Variables to pass information around between template renderings during a publish action. I just fired up some old code on a Tridion 2013 SP1 (HR1) system, and its not behaving as I expected.
I can see that if I set a Context Variable in a Page Template, that it is available in the Component Templates of Component Presentations (dynamic or static) embedded in the page, however if I set a Context Variable in the Component Template, its not available in the Page Template.
Also if I have multiple items in the publish transaction, the Context Variables are not shared between them.
Is this a bug, or has there been a functional change since Tridion 2011 in the way Context Variables are used?
I am testing with the following 2 simple C# TBBs:
Add Context Variable:
var cacheKey = "test-shared-data";
if (engine.PublishingContext.RenderContext != null)
{
if (engine.PublishingContext.RenderContext.ContextVariables.Contains(cacheKey))
{
engine.PublishingContext.RenderContext.ContextVariables.Remove(cacheKey);
}
engine.PublishingContext.RenderContext.ContextVariables.Add(cacheKey, "Hello World!");
}
Read Context Variable:
String cacheKey = "test-shared-data";
String data = "No context";
if (engine.PublishingContext.RenderContext != null)
{
if (engine.PublishingContext.RenderContext.ContextVariables.Contains(cacheKey))
{
data = (String)engine.PublishingContext.RenderContext.ContextVariables[cacheKey];
}
else
{ data = "No variable";}
}
package.PushItem("Output", package.CreateStringItem(ContentType.Text, data));