From f7c6276a7a7c90f4e2b5376126760e6b63fb7e8a Mon Sep 17 00:00:00 2001
From: Riyyi
Date: Sat, 3 Dec 2022 12:48:18 +0100
Subject: [PATCH] Controllers+Views: Move tag splitting logic to controller
---
app/controllers/BlogController.php | 15 +++++++++++++--
app/views/partials/blog-posts.php | 8 +-------
2 files changed, 14 insertions(+), 9 deletions(-)
diff --git a/app/controllers/BlogController.php b/app/controllers/BlogController.php
index a59e650..fc0d0b4 100644
--- a/app/controllers/BlogController.php
+++ b/app/controllers/BlogController.php
@@ -54,9 +54,20 @@ class BlogController extends PageController {
return "{$date}";
};
- $this->router->service()->tags = function (string $tags): array {
+ $this->router->service()->tags = function (string $tags): string {
+ $result = "";
+
// Remove empty elements via array_filter()
- return array_filter(explode(':', $tags));
+ $splitTags = array_filter(explode(':', $tags), function ($tag) {
+ return !empty(trim($tag));
+ });
+
+ foreach ($splitTags as $key => $tag) {
+ $result .= $tag;
+ $result .= (($key === array_key_last($splitTags)) ? '' : ', ');
+ }
+
+ return $result;
};
}
diff --git a/app/views/partials/blog-posts.php b/app/views/partials/blog-posts.php
index 9654423..97ca0d0 100644
--- a/app/views/partials/blog-posts.php
+++ b/app/views/partials/blog-posts.php
@@ -15,13 +15,7 @@
-
- tags:
- tags)($post['tag']); ?>
- $tag) { ?>
- = $tag . (($key === array_key_last($tags)) ? '' : ', '); ?>
-
-
+ tags: = ($this->tags)($post['tag']); ?>