November Happy Hour will be moved to Thursday December 5th.

How do I test for whether a particular block will be rendered on the current page?

Vote:
 

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?

if (Some.Optimizely.Hierarchy.Current.Blocks.Contains("FooBlock")

{

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

Thanks

--Bob

#267051
Nov 19, 2021 17:51
Vote:
 

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.

#267052
Nov 19, 2021 18:25
Vote:
 

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.

#267053
Edited, Nov 19, 2021 18:56
Vote:
 

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!

#267054
Nov 19, 2021 19:07
Vote:
 

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.

#267066
Nov 19, 2021 19:54
Scott Reed - Nov 20, 2021 22:21
yep I wasn't suggesting client resources for the solution just as a source of why things like this works. Assumed TempData would be the solution :-)
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.