Skip to content

Commit c331422

Browse files
Refactored event.html last 4 posts generator to work when less than 4 recent posts are available.
This needs to be further refactored to contain only the outer 4 loop and the inner one will be included into the event.html file if possible. Started creating logic to generate posts arrays for events page (based on event.html).
1 parent e0720ae commit c331422

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

_includes/event.html

+21-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ <h2 class="section-title">Recent Events</h2>
77
</div>
88

99
{% comment %}
10-
Create an array of last 4 posts (from newest to oldest) to show in recent events section.
11-
Since we also allow future posts to show upcoming event we have to eliminate the future ones
10+
Create an array of last 4 posts (if available, from newest to oldest)
11+
to show in recent events section. Since we also allow future posts
12+
to show upcoming event we have to eliminate the future ones
1213
from displaying in the past ones.
1314
{% endcomment %}
1415

@@ -26,13 +27,26 @@ <h2 class="section-title">Recent Events</h2>
2627
{% endif %}
2728
{% endfor %}
2829

29-
{% comment %}
30-
Create a 2 row by 2 column structure for 4 recent posts.
31-
{% endcomment %}
30+
{% assign num_of_posts = last_4_posts | size %}
31+
{% assign num_posts_remainder = num_of_posts | modulo: 2 %}
32+
33+
{% if num_posts_remainder == 0 %}
34+
{% assign num_of_posts_even = true %}
35+
{% assign row_num_modifier = 1 %}
36+
{% else %}
37+
{% assign num_of_posts_even = false %}
38+
{% assign row_num_modifier = 0 %}
39+
{% endif %}
3240

33-
{% for row in (0..1) %}
41+
{% assign num_of_rows = num_of_posts | divided_by: 2 | floor | minus: row_num_modifier %}
42+
{% assign items_per_row = 1 %}
43+
44+
{% for row in (0..num_of_rows) %}
45+
{% if forloop.last and num_of_posts_even == false %}
46+
{% assign items_per_row = 0 %}
47+
{% endif %}
3448
<div class="row">
35-
{% for col in (0..1) %}
49+
{% for col in (0..items_per_row) %}
3650
{% assign post_index = row | times: 2 | plus: col %}
3751
{% assign post = last_4_posts[post_index] %}
3852
<div class="no-gutters__col col-sm-6">

_includes/events_page.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{% comment %}
3+
This include is to generate a list of posts for use in views.
4+
Parameters:
5+
- future_events: (bool) Include future events in the array?
6+
If future_events is true, no past events will be added.
7+
If it is false, only past events are added.
8+
Default: false.
9+
- limit: (int) How many events to add to the array.
10+
Default: all (no limit).
11+
{% endcomment %}
12+
13+
{% if include.future_events %}
14+
{% assign future_events = include.future_events %}
15+
{% endif %}
16+
{% if include.limit %}
17+
{% assign limit = include.limit %}
18+
{% endif %}
19+
20+
{% assign post_count = 0 %}
21+
{% assign curDate = site.time | date: "%s" %}
22+
{% assign posts = "" | split: ',' %}
23+
{% for post in site.posts %}
24+
25+
26+
27+
{% assign postStartDate = post.date | date: "%s" %}
28+
{% if future_events | default: false %}
29+
{% if postStartDate > curDate %}
30+
{% assign posts = posts | push: post %}
31+
{% assign post_count = post_count | plus: 1 %}
32+
{% endif %}
33+
34+
{% if postStartDate <= curDate %}
35+
{% assign last_4_posts = last_4_posts | push: post %}
36+
{% assign post_count = post_count | plus: 1 %}
37+
{% endif %}
38+
{% if post_count == 4 %}
39+
{% break %}
40+
{% endif %}
41+
{% endfor %}
42+

0 commit comments

Comments
 (0)