4

Im trying to add Seo friendly pagination. This means I need two tags in the head section (For example if I was on page 2)

<link rel="prev" href="myurl?page=1" />
<link rel="next" href="myurl?page=3" />

But my pagination is part of a controller rendering. So I cant figure out how to update these tags from the controller. I need to hide the prev tag on the first page and hide the next tag on the last page. I need to hide both if the results only end up being one page. Some of my paging is server side, and some is client side. Regardless I can really only see how to do this with Javascript. The place where I know the current page and the if I am on the last page is in the controller which does not seem to have access to the head tag. I could have it be its own rendering tied to a page, but that means the page needs to know it has some sort of paged control on it, and I still down see how to know which page its on at any given time. I can get the current page from the query string, but that doesnt help to know if its the last page or not.

  • How do you determine what the last page is? You need to be able to know what the last page is in order to be able to compare another page with it – Zachary Kniebel Nov 9 '16 at 5:38
5

First off, you need to control the execution order of your controller renderings. You need the controller that renders the <head> element to render after the controller "which knows about paging".

One way to achieve this, is using a setup like this:

@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>

Using this technique, everything in the "MAIN" placeholder executes before anything in the "HEAD" placeholder.

Now you can, in your controller that knows paging, leave a "breadcrumb" for your "HEAD" controller (to write "prev" and "next" links) to pick up.

Something like:

HttpContext.Current.Items["prevUrl"] = "myurl?page=1";
HttpContext.Current.Items["nextUrl"] = "myurl?page=3";

Jeremy Davis wrote up a full example of this in his blog, when we last had a discussion on how to achieve this on Slack. Getting MVC Components to Communicate. It even uses pagination as an example.

  • Im curious what the repercussions of doing this with script tags would be... for example, if script tags were run after the main content was run? Would need to play around with this. – Ethan Schofer Nov 9 '16 at 16:14
  • In my last project I had "head", "topbody", "main" and "postbody" placeholders following above principles. To cater for things such as bundling/minify, postscripts and so on. Works really well, I find. – Mark Cassidy Nov 9 '16 at 22:17
0

I don't have a clear answer, as too many details are lacking, but I would do the following:

  • Create a PageNavigationManager, which handles the logic to determine on what page you are. Based on this info, you can render prev/next.
  • Create two renderings: one for the article/other info that you want to show. I assume that you need the pagination info on that rendering as well. The other one will be purely used to render the link tags in the head
  • done

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.