Reputation: 254
I'm just getting started with Orchard and writing a module for Google Tag Manager which means I need to add a JavaScript snippet at the start of the body tag for every page in our site.
In my FilterProvider OnResultExecuting I've got
var context = _workContextAccessor.GetContext();
var tags = context.Layout.Body;
var snippet = Shape.Snippet();
snippet.ContainerId = containerId;
tags.Add(snippet);
where Shape is the DefaultShapeFactory. I know that Shape.Add has a second argument for position, but I've tried ":before", ".5", "1" with no success. It always gets rendered at the end of the body, using the ThemeMachine theme with no modifications.
If I modify the theme and add a new zone to either the Document or Layout view at the start of the body and then add my shape to that it obviously appears there which is my solution for now, but this means that we'll have to ensure that any theme we build has that zone in the correct place.
How do I add shape in code to the Body and control its position?
Upvotes: 1
Views: 339
Reputation: 730
This code works:
context.Layout.Body.Items.Insert(0, snippet)
Your code does not work because of
Upvotes: 1