lllllll
lllllll

Reputation: 4855

Embed Ruby code within Slim's HTML attribute

I'm trying to generate a series of progress bars with random completion width, using the lovely Slim templating language.

This is the code I would like to generate:

.progress-info
  .progress
    span.progress-bar.progress-bar-success data-toggle='tooltip' style="width: 20%;"

However I would like to generate the style's width randomly, something like:

.progress-info
  .progress
    span.progress-bar.progress-bar-success data-toggle='tooltip' style="width: #{rand(50)+'%'};"

Of course this does not work, it's just to give an idea.

Upvotes: 1

Views: 1263

Answers (1)

matt
matt

Reputation: 79813

Your problem is with rand(50)+'%' – you’re trying to add a String ('%') to a Fixnum (the result of rand).

You can fix it with a call to to_s:

span.progress-bar.progress-bar-success data-toggle='tooltip' style="width: #{rand(50).to_s + '%'};"

or more simply by only including the call to rand in the interpolation:

span.progress-bar.progress-bar-success data-toggle='tooltip' style="width: #{rand(50)}%;"

Upvotes: 2

Related Questions