0

I have a JSON to declare in :javascript and I am not familiar with Ruby. The JSON is below:

:javascript
  var collections = {
    feed: [{'label': 'All', 'url': 'All', 'isSelected': false}]
  };

But I want the part below in feed variable to repeat 20 times:

{'label': 'All', 'url': 'All', 'isSelected': true}

How do I do that using Ruby in Haml?

Thanks.

5
  • Would be helpful if you post some of your code around. You can always use the <% %> and the <%= %> syntax inside haml Commented Oct 24, 2012 at 7:54
  • If you just want to repeat that line as a string, you can do something like this: <%= 20.times { "{'label': 'All', 'url': 'All', 'isSelected': true}" } %>. Not tested, but you should get the point Commented Oct 24, 2012 at 7:55
  • 1
    He said in haml, so there's no need of <%=. Just type = Commented Oct 24, 2012 at 7:57
  • @mischa, ups you're right, but the idea is the same ;) Commented Oct 24, 2012 at 8:00
  • I tried all above, <% %> just doesn't work in :javascript Commented Oct 25, 2012 at 2:45

1 Answer 1

1

One way:

:javascript
   var collections = {
     feed: [<%="{'label': 'All', 'url': 'All', 'isSelected': false}" * 20 %>]
   };

Another way:

:javascript
  var colections = {
    <%=(["feed: {'label': 'All', 'url': 'All', 'isSelected': false}"] * 20).join(',')%>
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.