0

Technologies: Django/Python, Coffeescript/Eco Templates/Backbone.js.

Eco templates: https://github.com/sstephenson/eco

I'm using .eco templates with backbone to populate my django views. So, Django has its ways of counting iterations, etc. -- which is great -- but how do I translate that into an .eco template syntax?

I want it to do output like this every 2 iterations until loop is finished:

   <div class="row">
       <div class="col-md-6">test</div>
       <div class="col-md-6">test</div>
   </div>

Here is the code I'm trying to use:

       <% for thing in @things.models: %>
               <% if forloop.counter|divisibleby:'2': %> ### so what would be the eco equivalent to something like this in django?
                        <div class="row">
                              <div class="col-md-6">test</div>
                              <div class="col-md-6">test</div>
                        </div>
                {% endif %}
      {% endfor %}

The .eco template gives me an error which I can't understand for the above code.

Michael Sebastian
  • 785
  • 3
  • 15
  • 33
  • This makes no sense. If you're writing a template using Eco, you need to use Eco functionality, not Django. Django's forloop and divisibleby tags are for Django templates. – Daniel Roseman Oct 22 '15 at 17:16
  • @DanielRoseman I'm not sure what the equivalent to that would be, that is why I put it in there (so people would know what I'm getting at - if that makes sense). I guess I should have clarified that. – Michael Sebastian Oct 22 '15 at 17:27

1 Answers1

1

I've never used - or even heard of - eco templates before, but looking at the documentation, it's clear that they are very different from Django templates in inspiration, and more to the point that they can support arbitrary CoffeeScript operations. So, rather than looking for "a way to do this in eco", you should be looking for a way to do this in CoffeeScript.

Again, I've never used CoffeeScript, but looks like this would work:

   <% for thing, i in @things.models: %>
           <% if i % 2 == 0 %>
                    <div class="row">
            <% end %>
                          <div class="col-md-6">test</div>
                          <div class="col-md-6">test</div>
           <% if (i + 1) % 2 == 0 %>
                    </div>
           <% end %>
  <% end %>
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • good point - I've been so deep in the eco trenches I forgot about just using straight coffeescript. So, I currently have 3 items but implementing the mod 2 approach you have above only displays 1 item. – Michael Sebastian Oct 22 '15 at 18:27
  • Thanks Daniel this works great. Also, you need to add the colon at the end of your second if statement above per .eco syntax. Thanks again... – Michael Sebastian Oct 22 '15 at 18:35