1

I'm trying to see if it's possible to iterate a variable in Jekyll for a testimonial block I'm implementing for a Jekyll site. Basically, I'd like to have an icon be multiplied by the number dictated in my collection. Is this even possible with liquid markdown? Here's a snippet:

{% assign star = "<i class="icon-star"></i>" %}
{% assign star = star | times:{{ testimonials.stars }} %}

I'm thinking there's better ways to do this, but I was curious what I could get away with front matter.

marcanuy
  • 23,118
  • 9
  • 64
  • 113
Sean Kelly
  • 901
  • 7
  • 27

2 Answers2

2

To do it iterating, you can use a for loop appending the desired string to a variable:

{% assign star = '<i class="icon-star"></i>' %}
{% assign result = '' %}
{% for time in testimonials.stars %}
 {% assign result = result | append: star%}
{% endfor %}
marcanuy
  • 23,118
  • 9
  • 64
  • 113
  • Ah! I never properly thanked you. This totally worked at the time I was writing the code. Thanks again. Better late than never! – Sean Kelly Aug 25 '17 at 02:46
1

The key is to use a for block. You don't have to write multiple assign statements.

The following will render a star per the front matter key rating in any page using the code.
Modify the for bock as required.

{% assign star = '<i class="icon-star"></i>' %}
<div class="rating">
  {% for count in (1..page.rating)) %}
    {{ star }}
  {% endfor %}
</div>

Ref: docs

ashmaroli
  • 5,209
  • 2
  • 14
  • 25