Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to Razor Section Helper In Sitecore

I am trying to include different scripts on different pages in Sitecore and can't seem to find a very good way of doing this. In a normal mvc project I could add the @Section{} helper and use that on different partial views to specify where I want those scripts in the layout view, but I haven't been able to find an equivalent for the way that Razor helper is implemented with Sitecore. I'd like to do this without using a place holder, I don't want to add a view in Sitecore every time I need to add a script file.

Thanks in advance.

like image 419
The Pax Bisonica Avatar asked Feb 13 '23 13:02

The Pax Bisonica


1 Answers

I'm afraid you're out of luck here.
@Section is not supported because Sitecore doesn't render the Razor views in the same way as MVC does.
A Sitecore MVC layout is basically just a regular view that is rendering several other partial views or controller actions.

So when the placeholders in the <body> of your layout view are being rendered, the <head> section of that layout has already been rendered.
There is no such thing as deferred rendering in Sitecore MVC like you can do with @Section.

Everything in the view is executed from top to bottom, so if you can put your scripts at the end of your layout (like before the </body>), you can still manipulate data in the views or actions that are executed earlier.

like image 172
Ruud van Falier Avatar answered Feb 16 '23 03:02

Ruud van Falier