From 4a699fd0b820f14fd61ae365935ac60f2a25560b Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Sat, 8 Jun 2024 15:50:45 +0200 Subject: [PATCH] Style pagination with Bootstrap --- src/Controller/PostController.php | 12 +++++++----- src/Repository/PostRepository.php | 6 ++++++ templates/_pagination.html.twig | 24 ++++++++++++++++++++++++ templates/base.html.twig | 3 +-- templates/post/index.html.twig | 24 ++++-------------------- 5 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 templates/_pagination.html.twig diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 4a55e24..e361797 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -6,20 +6,22 @@ use App\Repository\PostRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; class PostController extends AbstractController { + private const POSTS_PER_PAGE = 10; + #[Route('/', name: 'app_posts')] public function index(PostRepository $repository, Request $request): Response { - $page = $request->query->getInt('page',1); - $posts = $repository->findPaginatedPosts($page, 10); + $page = $request->query->getInt('page', 1); + $posts = $repository->findPaginatedPosts($page, self::POSTS_PER_PAGE); + $maxPage = ceil($posts->count() / self::POSTS_PER_PAGE); return $this->render('post/index.html.twig', [ 'posts' => $posts, - 'maxPosts' => $page * 10, - 'currentPage' => $page + 'maxPage' => $maxPage, + 'page' => $page, ]); } } diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php index be9dc1a..2082982 100644 --- a/src/Repository/PostRepository.php +++ b/src/Repository/PostRepository.php @@ -17,6 +17,11 @@ class PostRepository extends ServiceEntityRepository parent::__construct($registry, Post::class); } + /** + * @param int $page + * @param int $limit + * @return Paginator + */ public function findPaginatedPosts(int $page, int $limit): Paginator { $query = $this->createQueryBuilder('p') @@ -25,6 +30,7 @@ class PostRepository extends ServiceEntityRepository return new Paginator($query, fetchJoinCollection: false); } + // /** // * @return Post[] Returns an array of Post objects // */ diff --git a/templates/_pagination.html.twig b/templates/_pagination.html.twig new file mode 100644 index 0000000..1c4cc35 --- /dev/null +++ b/templates/_pagination.html.twig @@ -0,0 +1,24 @@ +{% set route = app.request.attributes.get('_route') %} + diff --git a/templates/base.html.twig b/templates/base.html.twig index d8dd28b..ec9dcfd 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -4,9 +4,8 @@ {% block title %}Welcome to Herbarium!{% endblock %} - - {% block stylesheets %} + {% endblock %} {% block javascripts %} diff --git a/templates/post/index.html.twig b/templates/post/index.html.twig index 6ca4bd2..82e3d39 100644 --- a/templates/post/index.html.twig +++ b/templates/post/index.html.twig @@ -2,35 +2,19 @@ {% block title %}Posts!{% endblock %} {% block body %} - - - - - - - - -Ajouter une espèce -Ajouter un post - {% for post in posts.iterator %}
-
User
+
{{ post.species ? post.species.vernacularName : 'Post' }}
{{ post.foundDate | date("d/m/Y \\à H \\h") }}

{{ post.latitude }}, {{ post.longitude }}, {{ post.altitude }}m

{{ post.commentary }}

{% endfor %} - - -{% if posts.count > maxPosts %} - Load More -{% endif %} - +{% include '_pagination.html.twig' %} {% endblock %}