-
-
Save tobbbe/26595348fa4e4d3a5ecf to your computer and use it in GitHub Desktop.
Umbraco route hijacking What's this Umbraco route hijacking all about? (more info in this blog post: http://cultiv.nl/blog/whats-this-umbraco-route-hijacking-all-about/)
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
// MODEL | |
using System.Collections.Generic; | |
using System.Linq; | |
using Umbraco.Core.Models; | |
using Umbraco.Web; | |
using Umbraco.Web.Models; | |
namespace Cultiv.Models | |
{ | |
public class BlogOverview : RenderModel | |
{ | |
public BlogOverview() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { } | |
public int Page { get; set; } | |
public int TotalPages { get; set; } | |
public int PreviousPage { get; set; } | |
public int NextPage { get; set; } | |
public bool IsFirstPage { get; set; } | |
public bool IsLastPage { get; set; } | |
public IEnumerable<IPublishedContent> BlogPosts { get; set; } | |
} | |
} | |
//CONTROLLER | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using Cultiv.Models; | |
using Umbraco.Core.Models; | |
using Umbraco.Web.Mvc; | |
namespace Cultiv.Controllers | |
{ | |
public class BlogOverviewController : RenderMvcController | |
{ | |
public ActionResult BlogOverview(BlogOverview model) | |
{ | |
model.BlogPosts = GetPagedBlogPosts(model); | |
return CurrentTemplate(model); | |
} | |
private static IEnumerable<IPublishedContent> GetPagedBlogPosts(BlogOverview model) | |
{ | |
if (model.Page == default(int)) | |
model.Page = 1; | |
const int pageSize = 5; | |
var skipItems = (pageSize*model.Page) - pageSize; | |
var posts = model.Content.Children.ToList(); | |
model.TotalPages = Convert.ToInt32(Math.Ceiling((double) posts.Count()/pageSize)); | |
model.PreviousPage = model.Page - 1; | |
model.NextPage = model.Page + 1; | |
model.IsFirstPage = model.Page <= 1; | |
model.IsLastPage = model.Page >= model.TotalPages; | |
return posts.OrderByDescending(x => x.CreateDate).Skip(skipItems).Take(pageSize); | |
} | |
} | |
} | |
//VIEW | |
@using Cultiv.Models | |
@inherits UmbracoViewPage<BlogOverview> | |
@{ | |
Layout = "~/Views/Site.cshtml"; | |
} | |
@foreach (var post in Model.BlogPosts) | |
{ | |
<h2 class="post-title" itemprop="name"><a itemprop="url" href="@post.Url">@post.Name</a></h2> | |
<p itemprop="articleBody">@Cultiv.StringHelpers.GetSummary(post.GetPropertyValue<string>("bodyText"), 50)...</p> | |
} | |
<section class="pagination-wrapper"> | |
<nav class="pagination" role="pagination"> | |
@if (Model.IsFirstPage == false) | |
{ | |
<a class="newer-posts" href="?page=@Model.PreviousPage">← Newer Posts</a> | |
} | |
<span class="page-number">Page <span class="number">@Model.Page</span> of <span class="number">@Model.TotalPages</span></span> | |
@if (Model.IsLastPage == false) | |
{ | |
<a class="older-posts" href="?page=@Model.NextPage">Older Posts →</a> | |
} | |
</nav> | |
</section> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment