Created
March 31, 2018 19:52
-
-
Save hartviglarsen/1c5960e157590090c0143684ba3343d8 to your computer and use it in GitHub Desktop.
Simple Umbraco pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Home> | |
@using ContentModels = Umbraco.Web.PublishedContentModels; | |
@{ | |
Layout = "Master.cshtml"; | |
Home home = Model.Content as Home; | |
int page = 1; | |
var postsPerPage = home.PostsPerPage; | |
if (Request.QueryString["page"] != null) | |
{ | |
int.TryParse(Request.QueryString["page"], out page); | |
} | |
var posts = home | |
.FirstChild<Blog>() | |
.Children<Post>() | |
.Where(x => x.IsVisible()) | |
.OrderByDescending(x => x.CreateDate); | |
int count = posts.Count(); | |
int pages = (int) Math.Ceiling((double) count / (double) postsPerPage); | |
} | |
@if (posts != null) | |
{ | |
foreach (Post post in posts.Skip((page - 1) * postsPerPage).Take(postsPerPage)) | |
{ | |
<article> | |
<h2>@post.Title</h2> | |
<div class="content"> | |
@Html.GetGridHtml(post, "bodyText", "bulma") | |
</div> | |
</article> | |
<hr> | |
} | |
} | |
else | |
{ | |
<p>No posts :-)</p> | |
} | |
@if (pages > 1) | |
{ | |
<nav class="pagination" role="navigation" aria-label="pagination"> | |
<ul class="pagination-list"> | |
@for (int p = 1; p < pages + 1; p++) | |
{ | |
<li> | |
<a class="pagination-link @(p == page ? "is-current" : string.Empty)" href="?page=@p"> | |
@p | |
</a> | |
</li> | |
} | |
</ul> | |
</nav> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment