From df911aa13e80d9530d64bc4f8a816fbcd16f3d8c Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Fri, 14 Sep 2012 15:36:57 +0200 Subject: [PATCH] Paginated blog listing Signed-off-by: Gergely Polonkai (W00d5t0ck) --- .../FrontBundle/Controller/BlogController.php | 31 +++++++++++++++++ .../FrontBundle/Resources/public/css/blog.css | 21 ++++++++++++ .../Resources/public/css/front.css | 10 +++--- .../Resources/views/Blog/list.html.twig | 33 +++++++++++++++++++ .../Resources/views/Blog/postViewer.html.twig | 8 +++-- .../views/Default/front_base.html.twig | 2 +- .../Resources/views/Default/index.html.twig | 3 ++ 7 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 src/GergelyPolonkai/FrontBundle/Resources/public/css/blog.css create mode 100644 src/GergelyPolonkai/FrontBundle/Resources/views/Blog/list.html.twig diff --git a/src/GergelyPolonkai/FrontBundle/Controller/BlogController.php b/src/GergelyPolonkai/FrontBundle/Controller/BlogController.php index d55ee03..40920f0 100644 --- a/src/GergelyPolonkai/FrontBundle/Controller/BlogController.php +++ b/src/GergelyPolonkai/FrontBundle/Controller/BlogController.php @@ -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 diff --git a/src/GergelyPolonkai/FrontBundle/Resources/public/css/blog.css b/src/GergelyPolonkai/FrontBundle/Resources/public/css/blog.css new file mode 100644 index 0000000..2378a51 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Resources/public/css/blog.css @@ -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; +} \ No newline at end of file diff --git a/src/GergelyPolonkai/FrontBundle/Resources/public/css/front.css b/src/GergelyPolonkai/FrontBundle/Resources/public/css/front.css index 21c4324..d8226bc 100644 --- a/src/GergelyPolonkai/FrontBundle/Resources/public/css/front.css +++ b/src/GergelyPolonkai/FrontBundle/Resources/public/css/front.css @@ -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; +} \ No newline at end of file diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Blog/list.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Blog/list.html.twig new file mode 100644 index 0000000..c35fd9f --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Blog/list.html.twig @@ -0,0 +1,33 @@ +{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %} + +{% block title %} - Blog{% endblock %} + +{% block paginator %} +{% if count > 1 %} +
+{% if cpage > 0 %} +First +{% endif %} +{% if cpage > 1 %} +Previous +{% endif %} +{% for i in 1..count %} +{% if cpage != i - 1 %}{% endif %}{{ i }}{% if cpage != i - 1 %} {% else %} {% endif %} +{% endfor %} +{% if cpage < count - 2 %} +Next +{% endif %} +{% if cpage < count - 1 %} +Last +{% endif %} +
+{% 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 %} \ No newline at end of file diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Blog/postViewer.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Blog/postViewer.html.twig index f087b33..b847317 100644 --- a/src/GergelyPolonkai/FrontBundle/Resources/views/Blog/postViewer.html.twig +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Blog/postViewer.html.twig @@ -1,3 +1,5 @@ -

{% if title_links %}{% endif %}{{ post.title }}{% if title_links %}{% endif %}

- -{{ post.content|insert_code_chunks }} +
+

{% if title_links %}{% endif %}{{ post.title }}{% if title_links %}{% endif %}

+ + {{ post.content|insert_code_chunks }} +
diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Default/front_base.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Default/front_base.html.twig index a7c59c7..87461a9 100644 --- a/src/GergelyPolonkai/FrontBundle/Resources/views/Default/front_base.html.twig +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Default/front_base.html.twig @@ -35,7 +35,7 @@ {% block content %}{% endblock content %} diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Default/index.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Default/index.html.twig index 62bef9c..ef0940c 100644 --- a/src/GergelyPolonkai/FrontBundle/Resources/views/Default/index.html.twig +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Default/index.html.twig @@ -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 %} + +{% endif %} {% endblock content %}