Paginated blog listing

Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2012-09-14 15:36:57 +02:00
parent fbee2e4c8d
commit df911aa13e
7 changed files with 98 additions and 10 deletions

View File

@ -4,6 +4,7 @@ namespace GergelyPolonkai\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Doctrine\ORM\Tools\Pagination\Paginator;
use GergelyPolonkai\FrontBundle\Entity\Post;
@ -16,6 +17,36 @@ use GergelyPolonkai\FrontBundle\Entity\Post;
*/
class BlogController extends Controller
{
/**
* @Route("/", name="GergelyPolonkaiFrontBundle_blogListing")
* @Route("/page/{cPage}", name="GergelyPolonkaiFrontBundle_blogListingPage", requirements={"cPage": "\d+"})
* @Template
*/
public function listAction($cPage = 1)
{
// TODO: Make this a config parameter
$postsPerPage = 10;
--$cPage;
$query = $this
->getDoctrine()
->getEntityManager()
->createQuery("SELECT p FROM GergelyPolonkaiFrontBundle:Post p WHERE p.draft = FALSE ORDER BY p.createdAt DESC")
->setFirstResult($cPage * $postsPerPage)
->setMaxResults($postsPerPage);
$paginator = new Paginator($query, $fetchJoinCollection = true);
$count = $paginator->count();
$pageCount = ceil($count / $postsPerPage);
return array(
'cpage' => $cPage,
'count' => $pageCount,
'posts' => $paginator,
'perPage' => $postsPerPage,
);
}
/**
* @Route("/{year}/{month}/{day}/{slug}.html", name="GergelyPolonkaiFront_blogViewPost")
* @Template

View File

@ -0,0 +1,21 @@
/*
Document : blog
Created on : 2012.09.14., 14:53:34
Author : polonkai.gergely
Description:
Purpose of the stylesheet follows.
*/
.post {
margin-bottom: 2em;
}
p.article-date {
text-indent: 0;
font-size: 80%;
color: #7f7f7f;
}
.paginator {
margin: .5em 0;
}

View File

@ -110,12 +110,6 @@ body {
text-decoration: underline;
}
#content p.article-date {
text-indent: 0;
font-size: 80%;
color: #7f7f7f;
}
dt {
font-weight: normal;
text-decoration: underline;
@ -168,3 +162,7 @@ dd p {
text-decoration: underline;
}
#more-posts {
margin-top: 1em;
text-align: right;
}

View File

@ -0,0 +1,33 @@
{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %}
{% block title %} - Blog{% endblock %}
{% block paginator %}
{% if count > 1 %}
<div class="paginator">
{% if cpage > 0 %}
<a href="{{ path('GergelyPolonkaiFrontBundle_blogListing') }}">First</a>
{% endif %}
{% if cpage > 1 %}
<a href="{{ path('GergelyPolonkaiFrontBundle_blogListingPage', {cPage: cpage}) }}">Previous</a>
{% endif %}
{% for i in 1..count %}
{% if cpage != i - 1 %}<a href="{{ (i == 1) ? path('GergelyPolonkaiFrontBundle_blogListing') : path('GergelyPolonkaiFrontBundle_blogListingPage', {cPage: i}) }}">{% endif %}{{ i }}{% if cpage != i - 1 %}</a> {% else %} {% endif %}
{% endfor %}
{% if cpage < count - 2 %}
<a href="{{ path('GergelyPolonkaiFrontBundle_blogListingPage', {cPage: cpage + 2}) }}">Next</a>
{% endif %}
{% if cpage < count - 1 %}
<a href="{{ path('GergelyPolonkaiFrontBundle_blogListingPage', {cPage: count}) }}">Last</a>
{% endif %}
</div>
{% endif %}
{% endblock paginator %}
{% block content %}
{{ block('paginator') }}
{% for post in posts %}
{% include 'GergelyPolonkaiFrontBundle:Blog:postViewer.html.twig' with {'post': post, 'title_links': true} %}
{% endfor %}
{{ block('paginator') }}
{% endblock content %}

View File

@ -1,3 +1,5 @@
<h3>{% if title_links %}<a href="{{ path('GergelyPolonkaiFront_blogViewPost', {year: post.createdAt|date('Y'), month: post.createdAt|date('m'), day: post.createdAt|date('d'), slug: post.slug}) }}">{% endif %}{{ post.title }}{% if title_links %}</a>{% endif %}</h3>
<p class="article-date">{{ post.createdAt|date('m-d-Y :: H:i') }} by {{ post.user.name }}</p>
{{ post.content|insert_code_chunks }}
<div class="post">
<h3>{% if title_links %}<a href="{{ path('GergelyPolonkaiFront_blogViewPost', {year: post.createdAt|date('Y'), month: post.createdAt|date('m'), day: post.createdAt|date('d'), slug: post.slug}) }}">{% endif %}{{ post.title }}{% if title_links %}</a>{% endif %}</h3>
<p class="article-date">{{ post.createdAt|date('m-d-Y :: H:i') }} by {{ post.user.name }}</p>
{{ post.content|insert_code_chunks }}
</div>

View File

@ -35,7 +35,7 @@
<div id="menu">
<ul>
<li><a href="{{ path('GergelyPolonkaiFrontBundle_about') }}">About me</a></li>
<li><a href="{{ path('GergelyPolonkaiFrontBundle_homepage') }}">Blog</a></li>
<li><a href="{{ path('GergelyPolonkaiFrontBundle_blogListing') }}">Blog</a></li>
</ul>
</div>
{% block content %}{% endblock content %}

View File

@ -1,7 +1,10 @@
{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %}
{% block content %}
{% if posts %}
{% for post in posts %}
{% include 'GergelyPolonkaiFrontBundle:Blog:postViewer.html.twig' with {'post': post, 'title_links': true} %}
{% endfor %}
<div id="more-posts"><a href="{{ path('GergelyPolonkaiFrontBundle_blogListing') }}">See all my posts</a></div>
{% endif %}
{% endblock content %}