This question is regarding HubSpot
/HuBl
, but I am also tagging twig
as the syntaxes are similar.
I have a blog layout containing 7 cards and has the following structure:
For reference, the class modifiers for the above are:
blogCard
blogCard
blogCard--long
blogCard
blogCard
blogCard--wide
blogCard
Now, in HubL
, I'm trying to loop though posts and where the loop.index
is equal to three
, I want it to show a blog post which has the tag of video
. This video
post will appear in blogCard--long
.
So all other 6 cards can show posts with any tag, but the 3rd (blogCard--long
) must be a post with the tag of video
.
To achieve this, I have tried the following:
<!-- get all posts from blog -->
{% set all_posts = blog_recent_posts(blog_id, 21) %}
<!-- find all posts with the tag "video" -->
{% set video_posts = blog_recent_tag_posts(blog_id, 'video') %}
<!-- remove video posts from all_posts array -->
{% set posts = all_posts|difference(video_posts) %}
{% for post in posts %}
<div class="blogCard"></div>
{% if (loop.index == 3) %}
{% for video_post in video_posts %}
<div class="blogCard--long">{{ video_post.name }}</div>
{% endfor %}
{% endif %}
<div class="blogCard"></div>
{% if loop.index == 6 %}
<div class="latestBlogs__blog--wide"></div>
{% endif %}
<div class="blogCard"></div>
{% endfor %}
I am essentially running the initial posts
loop on all posts excluding those tagged with video
. Then, when encountering the third item, I'm running a nested loop to obtain data from a video post. However, since it is a nested loop, it shows all of the video posts in one loop iterations. For example, if I have two posts that are tagged with video
, on one iteration, it will show both those video posts.
Just looking to advice on the logic I'm applying here and what the best way would be to achieve this?
After reading through the comments, you could use batch to repeat the grid. I would do something like the following:
demo
The first line,
{% for row in blogs|batch(6, ' ') %}
will turn your collection in an array of 6 items each, which you then can loop to create one full grid.If the index equals 3 you then can "prepend" one video post. Here I use the loop variable from the first loop:
This index you then can use to fetch a video on that index, chained with the default filter if the videos is non-existent