templates/website/news/actualites.html.twig line 1

Open in your IDE?
  1. {% extends "website/base.html.twig" %}
  2. {% block title %}Actualités{% endblock %}
  3. {% block body %}
  4.     
  5. <div class="container searchnews">
  6.     <div class="row">
  7.         <div class="col">
  8.             <div class="searchnews__inner">
  9.                 <input type="text" id="searchnews_bar" placeholder="Recherchez une actualité">
  10.                 <button id="searchnews_btn"><img src="website/Assets/loupe.svg" alt="icone de loupe"></button>
  11.             </div>
  12.         </div>
  13.     </div>
  14. </div>
  15. <div id="searchnews_container">
  16.     {% for new in news %}
  17.         <div class="container news">
  18.             <div class="row">
  19.                 <div class="col">
  20.                     <figure class="news__inner">
  21.                         <h3>{{ new.eventDate|date('d/m/Y') }}</h3>
  22.                         <section class="news__img">
  23.                             
  24.                             {% for img in new.images %}
  25.                                 <img src="/assets/images/{{img.id}}.{{img.extension}}" alt="image d'actualité">
  26.                             {% endfor %}
  27.                         </section>
  28.                         <figcaption>
  29.                             <img src="website/Assets/logo.png" alt="Logo de La Marguerite">
  30.                             <div class="news__content">
  31.                                 <h2>{{ new.title }}</h2>
  32.                                 <div>{{ new.body | raw}}</div>
  33.                             </div>
  34.                         </figcaption>
  35.                     </figure>
  36.                 </div>
  37.             </div>
  38.         </div>
  39.     {% endfor %}
  40. </div>
  41. {{ include("website/news/article_card_template.html.twig") }}
  42. {% endblock %}
  43. {% block javascripts %}
  44. <script>
  45.     
  46.     $('#searchnews_bar').next('button').click(function(e) {
  47.         e.preventDefault();
  48.         // Get the value of the searchbar
  49.         var searchTerm = $('#searchnews_bar').val();
  50.         $.ajax({
  51.             url: '/api/search/article/content?value=' + searchTerm, 
  52.             method: 'GET',
  53.             success: function(data) {
  54.                 // Replace the container with the new articles
  55.                 replaceArticleCards(data);
  56.             }
  57.         });
  58.     });
  59.     $('#searchnews_bar').keypress(function(e) {
  60.         if(e.which == 13) {
  61.             // If enter key is pressed
  62.             e.preventDefault();
  63.             // Get the value of the searchbar
  64.             var searchTerm = $('#searchnews_bar').val();
  65.             $.ajax({
  66.                 url: '/search/article/content?value=' + searchTerm, 
  67.                 method: 'GET',
  68.                 success: function(data) {
  69.                     // Replace the container with the new articles
  70.                     replaceArticleCards(data);
  71.                 }
  72.             });
  73.         }
  74.     });
  75.     function replaceArticleCards(articles) {
  76.         let listHTML = "";
  77.         // For each table of data, create a card and add it to the list
  78.         articles.forEach(article => {
  79.             listHTML +=createArticleCard(article);
  80.         });
  81.         // Replace the container content with all the cards created
  82.         $('#searchnews_container').html(listHTML);
  83.     }
  84.     function createArticleCard(article) {
  85.         // Get the templates of the card and the images
  86.         var template_article_card = $('#article_card_template').html();
  87.         var template_article_card_image = $('#article_card_image_template').html();
  88.         // Template the images
  89.         var imageHTML = "";
  90.         article.image.forEach(image => {
  91.             imageHTML += template_article_card_image.replace(/%image%/g, image.image);
  92.         });
  93.         // Format the date
  94.         var date = new Date(article.date.date);
  95.         var formattedDate = date.getDate().toString().padStart(2, '0') + '/' + (date.getMonth()+1).toString().padStart(2, '0') + '/' + date.getFullYear();
  96.         // Template the card
  97.         var listHTML=template_article_card.replace(/%title%/g, article.title).replace(/%body%/g, article.body).replace(/%date%/g, formattedDate).replace(/%image%/g, imageHTML);
  98.         return listHTML;
  99.     }
  100.     
  101. </script>
  102. {% endblock %}