4

How do you repeat string multiple times in Jekyll?

For eg: in Python, you can do "hello" * 5 to get 'hellohellohellohellohello'. In Jekyll is there an easier way of doing the same thing than this.

{% for i in (1..5) %}
  Hello
{% endfor %}

Example

Say you want to display a star rating and you are using <i class="fa fa-star"></i> to display each star. So to display a 5 star rating you would do.

{% for i in (1..5) %}
    <i class="fa fa-star"></i>
{% endfor %}

A single page may have many ratings displayed on it so you would be using several instances of that for loop.

Ishan
  • 2,935
  • 11
  • 30
  • 50
  • No, that is the easiest way. – marcanuy Mar 17 '17 at 00:27
  • Can you provide a more concrete use-case to help request this feature at the official repository? – ashmaroli Mar 17 '17 at 07:06
  • @ashmaroli. Updated my question with an example use case. – Ishan Mar 18 '17 at 00:54
  • I was expecting to see an example of repeated "string" but instead see a case of repeated "HTML tags" `` for which, the current `for` loop is the best. You may simply extract that to an `include` and add that snippet as a single line of code (with an additional parameter signifying an active star (e.g. rating: 3 out of 5 )). – ashmaroli Mar 18 '17 at 02:03

1 Answers1

0

This is the best way.

If your concern is about performance; then you don't need worry because Jekyll Liquid is precompiled into html anyway so the performance will only be affected at build time.

Buts
  • 2,063
  • 1
  • 25
  • 41