My use case is simple: If a block, FooBlock, is to be rendered on a page, then I need to inject some content into both FooBlock and the page header. Our header is rendered in view HeadPartial.cshmtl. The content that's going into the header (but only needed if FooBlock will be present further down) is a decent-sized payload of meta tags and a <script> block, so I really don't want this rendered on all of our pages.
Is there something along the lines of this in the API that I could use in HeadPartial.cshtml?
// insert my header content
I realize that, being Optimizely, it's going to be like six lines of code, but I'm just trying to convey what I actually need here.
Of note: the header content is returned by an SDK call into a vendor dll, it is dynamic and i have no control over it. So it's not some static content that I can paste from authoring mode. Because it includes meta tags, it has to go in the header, it is not just a <script> block I could inject into FooBlock.
BTW, if it matters, we're using ASP.NET MVC. ASP.NET Core solutions, if they're different, are welcome, as we'll be migrating soon, but I would prefer to solve this in a way that works with Optimizely 11 / ASP.NET MVC.
Blocks are executed before the page renderes which is why you can use client resources https://world.optimizely.com/documentation/developer-guides/CMS/client-resources/
So you can therefore use TempData. For example this code running on a block controller
TempData["MyBlock"] = true;
Will render out true if referenced in the master page or a partial from the master template when the block is executed. This accepts any object so you could build a little class for managing this or just pass a value for each block.
I was wondering why my breakpoint in the block was hitting before my breakpoint in the header. I'm a bit of a noob on this platform so still getting my bearings. Thanks for this, I will try it out and let you know how it goes!
Thanks @Scott Reed. I did not need client resources as such. I simply set a flag in TempData in the block controller and checked that in the HeaderPartial code to see if I need to inject the header stuff. Works perfectly.