2

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));

1 Answer 1

3

As far as I know, this hasn't changed since 2011. From the very beginning of compound templating, the context variables dictionary that you get in a component render is a copy of the context variables from the page render.

For a workaround, please see this article at Tridion Practice.

5
  • 2
    I never realized that the CT context variables were a copy - that explains it! The code I was using in 2011 was actually slightly different, and worked with a dictionary, so thats probably it worked - though I never understood why.
    – Will Price
    Apr 25, 2016 at 9:11
  • 1
    Beware that the copy is not done for DCPs in Publish mode.
    – hoang
    Apr 25, 2016 at 10:03
  • Do you mean that each DCP in a publish transaction gets its own isolated set of context variables, or that they share the same set?
    – Will Price
    Apr 25, 2016 at 11:14
  • 2
    Each DCP gets an isolated set of context variables.
    – hoang
    Apr 25, 2016 at 11:25
  • 1
    In order to implement data sharing across Dynamic Component Templates, I created some extension methods on PublishingContext. See this post for more: tridiondeveloper.com/…
    – Will Price
    Apr 29, 2016 at 12:10

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .