CMS Development

MrCapp
Colaborador

Assign different classes to alternating blog posts?

resolver

Hello there.

I'm trying to figure out how to apply two different css classes to my blog post entries as they are generated. 

 

In the code below, I have  <div class="post-item"> and I would like to append a class to each instance to affect the width of the post.

 

So the first one would be: <div class="post-item width-one-third"> and the second would be <div class="post-item two-thirds">.

 

I'm still feeling my way around the HUBL markup so I'm not sure how to approach this.

 

Any help is appreciated.

 

 

{% for content in contents %}
                <div class="post-item">
                    {% if not simple_list_page %}
                    <div class="post-item-inner">
                       <div class="features-image-block">                         
                      <a href="{{content.absolute_url}}">...

 

 

1 Soluciones aceptada
lgerodiaz
Solución
Colaborador

Assign different classes to alternating blog posts?

resolver

@MrCapp I think you could further use the 'cycle' tag in this case as it's not limited only up to two values. In fact, we could add multiple. For example:

 

{% for content in contents %}
    <div class="post-item {% cycle 'width-one-third','width-two-thirds', 'width-two-thirds','width-one-third' %}">

Which would generate an HTML like this:

<! -- start of cycle -->
<div class="post-item width-one-third">
<div class="post-item width-two-thirds">
<div class="post-item width-two-thirds">
<div class="post-item width-one-third">
<! -- cycle will repeat again -->
<div class="post-item width-one-third">
<div class="post-item width-two-thirds">
<div class="post-item width-two-thirds">
<div class="post-item width-one-third">

So on and so forth.

 

L

Ver la solución en mensaje original publicado

11 Respuestas 11
lgerodiaz
Colaborador

Assign different classes to alternating blog posts?

resolver

Hi,

 

You can use the 'cycle' tag for that base on HubSpot's Designer documentation.

 

To help you get started, use this:

{% for content in contents %}
    <div class="post-item {% cycle 'width-one-third','width-two-thirds' %}">

Let me know if it worked like a charm. Happy coding!

 

L

MrCapp
Colaborador

Assign different classes to alternating blog posts?

resolver

Hey lgerodiaz - thanks for your answer - that worked perfectly. That seems to be a simpler method than tjoyce's answer although that also worked just fine.

 

However what I didn't realize was that those classes would be applied the same way for each row of content.

 

Now I need the classes to reverse for the following row and then keep going back and forth until all the rows of content have been generated. Does that make sense?

 

The layout on the Hubspot blog shows what I'm trying to get to.

 

hubbbbb.png

 

 

 

 

Any idea on how I can do that?

lgerodiaz
Colaborador

Assign different classes to alternating blog posts?

resolver

If you want the items to be grouped inside a row, you can use this code instead

{% for content in contents %}
    {{ '<div class="row">' if loop.index % 2 != 0 }}

        <div class="post-item {% cycle 'width-one-third','width-two-thirds', 'width-two-thirds','width-one-third' %}">
            ...
        </div>

    {{ '</div>' if loop.index % 2 == 0 }}
{% endfor %}
0 Me gusta
lgerodiaz
Solución
Colaborador

Assign different classes to alternating blog posts?

resolver

@MrCapp I think you could further use the 'cycle' tag in this case as it's not limited only up to two values. In fact, we could add multiple. For example:

 

{% for content in contents %}
    <div class="post-item {% cycle 'width-one-third','width-two-thirds', 'width-two-thirds','width-one-third' %}">

Which would generate an HTML like this:

<! -- start of cycle -->
<div class="post-item width-one-third">
<div class="post-item width-two-thirds">
<div class="post-item width-two-thirds">
<div class="post-item width-one-third">
<! -- cycle will repeat again -->
<div class="post-item width-one-third">
<div class="post-item width-two-thirds">
<div class="post-item width-two-thirds">
<div class="post-item width-one-third">

So on and so forth.

 

L

MrCapp
Colaborador

Assign different classes to alternating blog posts?

resolver

Thanks again. That did the trick. 

For some reason the most recent post is not appearing as the first post in the list and it's affecting the layout. *sigh*

 

I think once I get that sorted the layout should work as expected.

 

Thanks again.

0 Me gusta
tjoyce
Experto reconocido | Partner nivel Elite
Experto reconocido | Partner nivel Elite

Assign different classes to alternating blog posts?

resolver

It is possible with javascript. I put together a sample for you here. The equation would be (4n - 1) and (4n - 2) in order to get your sequence.

Here's the fiddle

 


If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

0 Me gusta
MrCapp
Colaborador

Assign different classes to alternating blog posts?

resolver

Thanks for the suggestion. I think that would work but I'm trying to avoid JS for this bit.

0 Me gusta
tjoyce
Experto reconocido | Partner nivel Elite
Experto reconocido | Partner nivel Elite

Assign different classes to alternating blog posts?

resolver

@lgerodiaz - good find... didn't know about that one.

radar
Colaborador

Assign different classes to alternating blog posts?

resolver

@MrCapp you could use a bit jquery to do this. Something like this could work:

 

$(document).ready(function() {

  $('.blog .post-item:odd').addClass('width-one-third');
  $('.blog .post-item:even').addClass('wdith-two-thirds');

});

 

0 Me gusta
radar
Colaborador

Assign different classes to alternating blog posts?

resolver

Don't mind the emoji haha

0 Me gusta
tjoyce
Experto reconocido | Partner nivel Elite
Experto reconocido | Partner nivel Elite

Assign different classes to alternating blog posts?

resolver

Hello @MrCapp - You can use the modulus (%)  operator for that: 

{% for content in contents %}
  {% set extraClass = 'width-one-third' %}
  {% if loop.index % 2 == 0 %}
      {% set extraClass = 'width-two-third' %}
  {% endif %}
  <div class="post-item {{extraClass}}">...</div>
{% endfor %}

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

0 Me gusta