Ajout htmx

This commit is contained in:
Gilles Van Assche 2025-01-07 09:37:26 +01:00
parent d7590b4603
commit 4e02f23fde
7 changed files with 196 additions and 3 deletions

View File

@ -574,15 +574,24 @@ video {
.mb-8{
margin-bottom: 2rem
}
.mr-3{
margin-right: 0.75rem
}
.mt-4{
margin-top: 1rem
}
.mt-8{
margin-top: 2rem
}
.block{
display: block
}
.inline-block{
display: inline-block
}
.inline{
display: inline
}
.flex{
display: flex
}
@ -648,6 +657,15 @@ video {
.transform{
transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))
}
@keyframes spin{
to{
transform: rotate(360deg)
}
}
.animate-spin{
animation: spin 1s linear infinite
}
.cursor-pointer{
cursor: pointer
}
@ -832,6 +850,10 @@ video {
--tw-text-opacity: 1;
color: rgb(17 24 39 / var(--tw-text-opacity))
}
.text-slate-400{
--tw-text-opacity: 1;
color: rgb(148 163 184 / var(--tw-text-opacity))
}
.text-white{
--tw-text-opacity: 1;
color: rgb(255 255 255 / var(--tw-text-opacity))
@ -957,6 +979,9 @@ article > *:not(.entry-content),
list-style-position: inside;
list-style-type: decimal
}
.htmx-request .htmx-indicator{
display: inline
}
#loginform-custom label{
margin-bottom: 0.5rem;
display: block

View File

@ -77,12 +77,21 @@
.mt-4 {
margin-top: 1rem
}
.mt-8 {
margin-top: 2rem
}
.mr-3 {
margin-right: 0.75rem
}
.block {
display: block
}
.inline-block {
display: inline-block
}
.inline {
display: inline
}
.flex {
display: flex
}
@ -104,6 +113,9 @@
.h-full {
height: 100%
}
.h-5 {
height: 1.25rem
}
.min-h-12 {
min-height: 3rem
}
@ -122,6 +134,9 @@
.w-full {
width: 100%
}
.w-5 {
width: 1.25rem
}
.min-w-max {
min-width: -moz-max-content;
min-width: max-content
@ -148,6 +163,14 @@
.transform {
transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))
}
@keyframes spin {
to {
transform: rotate(360deg)
}
}
.animate-spin {
animation: spin 1s linear infinite
}
.cursor-pointer {
cursor: pointer
}
@ -336,6 +359,22 @@
--tw-text-opacity: 1;
color: rgb(255 255 255 / var(--tw-text-opacity))
}
.text-amber-500 {
--tw-text-opacity: 1;
color: rgb(245 158 11 / var(--tw-text-opacity))
}
.text-gray-300 {
--tw-text-opacity: 1;
color: rgb(209 213 219 / var(--tw-text-opacity))
}
.text-slate-200 {
--tw-text-opacity: 1;
color: rgb(226 232 240 / var(--tw-text-opacity))
}
.text-slate-400 {
--tw-text-opacity: 1;
color: rgb(148 163 184 / var(--tw-text-opacity))
}
.underline {
text-decoration-line: underline
}

View File

@ -112,3 +112,4 @@ add_filter( 'nav_menu_submenu_css_class', 'tailpress_nav_menu_add_submenu_class'
require_once(__DIR__ . '/includes/post-type.php');
require_once(__DIR__ . '/includes/add-form.php');
require_once(__DIR__ . '/includes/api.php');

63
htmx/events-home.php Normal file
View File

@ -0,0 +1,63 @@
<?php
$paged = $_GET['paged'];
//echo $paged;
// WP_Query arguments
$args = array(
'post_type' => 'evenements',
'posts_per_page' => 6,
'paged' => $paged,
'meta_key' => 'date_debut', // Nom du champ ACF
'orderby' => 'meta_value', // Trier par la valeur du champ
'order' => 'ASC', // Ordre croissant ('DESC' pour décroissant)
'meta_type' => 'DATE' // Préciser que c'est une date (important)
);
// The Query
$query = new WP_Query( $args );
?>
<tbody id="posts" hx-swap-oob="beforeend">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$date_debut = get_field('date_debut');
$date_fin = get_field('date_fin');
$code_postal = get_field('code_postal');
$organisme = get_field('organisme');
$lien_de_levenement = get_field('lien_de_levenement');
?>
<tr>
<td class="p-4 border-b border-slate-300"><?php echo $date_debut; ?></td>
<td class="p-4 border-b border-slate-300"><?php echo $date_fin; ?></td>
<td class="p-4 border-b border-slate-300"><?php echo $code_postal; ?></td>
<td class="p-4 border-b border-slate-300"><?php echo $organisme; ?></td>
<td class="p-4 border-b border-slate-300"><a href="<?php echo $lien_de_levenement; ?>" target="_blank"><?php the_title(); ?></a></td>
</tr>
<?php endwhile; else : ?>
<p><?php esc_html_e( "Il n'y a plus d'événements à charger." ); ?></p>
<?php endif; ?>
</tbody>
<?php
if ( $query->have_posts() ) :
?>
<button
id="more"
hx-get="/wp-json/htmx/v1/events-home?paged=<?php echo $paged+1; ?>"
hx-trigger="revealed"
hx-swap-oob="true"
class="bg-secondary py-2 px-4 mt-8 rounded-md text-white cursor-pointer mx-auto">
<svg class="text-slate-400 animate-spin inline mr-3 htmx-indicator hidden" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg"
width="18" height="18">
<path
d="M32 3C35.8083 3 39.5794 3.75011 43.0978 5.20749C46.6163 6.66488 49.8132 8.80101 52.5061 11.4939C55.199 14.1868 57.3351 17.3837 58.7925 20.9022C60.2499 24.4206 61 28.1917 61 32C61 35.8083 60.2499 39.5794 58.7925 43.0978C57.3351 46.6163 55.199 49.8132 52.5061 52.5061C49.8132 55.199 46.6163 57.3351 43.0978 58.7925C39.5794 60.2499 35.8083 61 32 61C28.1917 61 24.4206 60.2499 20.9022 58.7925C17.3837 57.3351 14.1868 55.199 11.4939 52.5061C8.801 49.8132 6.66487 46.6163 5.20749 43.0978C3.7501 39.5794 3 35.8083 3 32C3 28.1917 3.75011 24.4206 5.2075 20.9022C6.66489 17.3837 8.80101 14.1868 11.4939 11.4939C14.1868 8.80099 17.3838 6.66487 20.9022 5.20749C24.4206 3.7501 28.1917 3 32 3L32 3Z"
stroke="currentColor" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"></path>
<path
d="M32 3C36.5778 3 41.0906 4.08374 45.1692 6.16256C49.2477 8.24138 52.7762 11.2562 55.466 14.9605C58.1558 18.6647 59.9304 22.9531 60.6448 27.4748C61.3591 31.9965 60.9928 36.6232 59.5759 40.9762"
stroke="currentColor" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"
class="text-white"></path>
</svg>
Plus d'événements
</button>
<?php
endif;
?>

40
includes/api.php Normal file
View File

@ -0,0 +1,40 @@
<?php
// AJOUT ROUTE POUR HTMX
add_action('rest_api_init', function () {
register_rest_route('htmx/v1', '/events-home', [
'methods' => 'GET',
'callback' => 'load_custom_template',
'permission_callback' => '__return_true', // Autorise l'accès à tous les utilisateurs
]);
});
function load_custom_template($request) {
// Nom du fichier de template à inclure (ex. : 'custom-template.php' dans votre thème)
$template = locate_template('htmx/events-home.php');
if ($template) {
// Capture la sortie du template pour l'inclure dans la réponse
ob_start();
include $template;
$content = ob_get_clean();
header('Content-Type: text/html; charset=UTF-8');
echo $content;
exit; // Important pour éviter que WordPress ajoute des données JSON supplémentaires
}
// Retourne une erreur si le fichier template est introuvable
header('Content-Type: text/html; charset=UTF-8', true, 404);
echo 'Template introuvable.';
exit;
}
// ENQUEUE HTMX
add_action('wp_enqueue_scripts', 'add_scripts');
function add_scripts(){
wp_register_script( 'htmx', 'https://unpkg.com/htmx.org@2.0.3', false, null, true);
if(is_front_page()):
wp_enqueue_script( 'htmx' );
endif;
}

View File

@ -40,3 +40,8 @@ article > *:not(.entry-content),
}
}
}
.htmx-request .htmx-indicator{
@apply inline;
}

View File

@ -3,7 +3,7 @@
// WP_Query arguments
$args = array(
'post_type' => 'evenements',
'posts_per_page' => -1,
'posts_per_page' => 6,
'meta_key' => 'date_debut', // Nom du champ ACF
'orderby' => 'meta_value', // Trier par la valeur du champ
'order' => 'ASC', // Ordre croissant ('DESC' pour décroissant)
@ -12,6 +12,7 @@ $args = array(
// The Query
$query = new WP_Query( $args );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>
@ -27,7 +28,7 @@ $query = new WP_Query( $args );
<th class="p-4 border-b border-slate-300 bg-slate-200">Titre</th>
</tr>
</thead>
<tbody>
<tbody id="posts">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$date_debut = get_field('date_debut');
@ -44,9 +45,28 @@ $query = new WP_Query( $args );
<td class="p-4 border-b border-slate-300"><a href="<?php echo $lien_de_levenement; ?>" target="_blank"><?php the_title(); ?></a></td>
</tr>
<?php endwhile; else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<p><?php esc_html_e( "Il n'y a plus d'événements à charger." ); ?></p>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="pagination flex flex-col items-center">
<button
id="more"
hx-get="/wp-json/htmx/v1/events-home?paged=<?php echo $paged+1; ?>"
hx-trigger="revealed"
class="bg-secondary py-2 px-4 mt-8 rounded-md text-white cursor-pointer mx-auto">
<svg class="text-slate-400 animate-spin inline mr-3 htmx-indicator hidden" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg"
width="18" height="18">
<path
d="M32 3C35.8083 3 39.5794 3.75011 43.0978 5.20749C46.6163 6.66488 49.8132 8.80101 52.5061 11.4939C55.199 14.1868 57.3351 17.3837 58.7925 20.9022C60.2499 24.4206 61 28.1917 61 32C61 35.8083 60.2499 39.5794 58.7925 43.0978C57.3351 46.6163 55.199 49.8132 52.5061 52.5061C49.8132 55.199 46.6163 57.3351 43.0978 58.7925C39.5794 60.2499 35.8083 61 32 61C28.1917 61 24.4206 60.2499 20.9022 58.7925C17.3837 57.3351 14.1868 55.199 11.4939 52.5061C8.801 49.8132 6.66487 46.6163 5.20749 43.0978C3.7501 39.5794 3 35.8083 3 32C3 28.1917 3.75011 24.4206 5.2075 20.9022C6.66489 17.3837 8.80101 14.1868 11.4939 11.4939C14.1868 8.80099 17.3838 6.66487 20.9022 5.20749C24.4206 3.7501 28.1917 3 32 3L32 3Z"
stroke="currentColor" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"></path>
<path
d="M32 3C36.5778 3 41.0906 4.08374 45.1692 6.16256C49.2477 8.24138 52.7762 11.2562 55.466 14.9605C58.1558 18.6647 59.9304 22.9531 60.6448 27.4748C61.3591 31.9965 60.9928 36.6232 59.5759 40.9762"
stroke="currentColor" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"
class="text-white"></path>
</svg>
Plus d'événements
</button>
</div>
</section>