5

I know that MVC and by definition Sitecore MVC renders components from top to bottom order, but is there any way to force (hack) a controller rendering (which is used in the head of the page) to be rendered after another specific rendering lower down in the page?

I know this kind of makes an implied dependency which is crappy and generally goes against the philosophy of MVC, but I don't see another way around this particular problem. All the markup needs to be in place in the page at the point of page load, so a JavaScript-based solution won't work here, unfortunately.

5

Yep. There are various ways to go about it. Here is one.

@using Sitecore.Mvc
@{
    Layout = null;

    HtmlString main = Html.Sitecore().Placeholder("MAIN");
    HtmlString head = Html.Sitecore().Placeholder("HEAD");
}
<!DOCTYPE html>
<html>

<head>
    <title>@Html.Sitecore().Field("title", new { DisableWebEdit = true })</title>
    @head
</head>

<body>
    <h1>@Html.Sitecore().Field("title")</h1>
    <h2>Cross component comms layout</h2>
    <div>
        @main
    </div>
</body>

</html

Here, main is rendered before head (which is what you ask for), yet still output in the correct order in the document.

Source: GETTING MVC COMPONENTS TO COMMUNICATE

Additional reference:

  • 4
    Dammit! I see a question I could answer and you've already linked to my blog post on the topic! I am too slow for this malarky... ;-) – JermDavis May 15 '18 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.