Overriding the Pager rendering in Orchard

The Pager shape that is used in Orchard to render pagination is one of those shapes that are built in code rather than in a Razor template. This can make it a little more confusing to override, but nothing is impossible.

If we look at the Pager method in CoreShapes, here is what we see:

public IHtmlString Pager(dynamic Shape, dynamic Display) {
    Shape.Metadata.Type = "Pager_Links";
    return Display(Shape);

The Shape attribute signals a shape method. All it does is remove all alternates that may exist and replace the type of the shape with “Pager_Links”. In turn, this shape method is rather large and complicated, but it renders as a set of smaller shapes: a List with a “pager” class, and under that Pager_First, Pager_Previous, Pager_Gap, for each page a Pager_Link or a Pager_Current, then Pager_Gap, Pager_Next and Pager_Last.

Each of these shapes can be displayed or not depending on the properties of the pager. Each can also be overridden with a Razor template. This can be done by dropping a file into the Views folder of your theme. For example, if you want the current page to appear between square braces, you could drop this Pager-CurrentPage.cshtml into your views folder:


This overrides the original shape method, which was this:

public IHtmlString Pager_CurrentPage(HtmlHelper Html, dynamic Display,
    object Value) {
    var tagBuilder = new TagBuilder("span");
    tagBuilder.InnerHtml = Html.Encode(Value is string ?
        (string)Value : Display(Value));
    return MvcHtmlString.Create(tagBuilder.ToString());

And here is what it would look like:An overridden current page template

Now what if we want to completely hide the pager if there is only one page? Well, the easiest way to do that is to override the Pager shape by dropping the following into the Views folder of your theme:

    if (Model.TotalItemCount > Model.PageSize) {
        Model.Metadata.Type = "Pager_Links";

And that’s it. The code in this template just adds a check for the number of items to display (in a template, Model is the shape) and only displays the Pager_Links shape if it knows that there’s going to be more than one page.


Comments have been disabled for this content.