1
votes

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.

2

2 Answers

2
votes

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 %}
1
votes

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