Search code examples
djangocoffeescriptdjango-templateseco

Using Django template divisibilityby tag with .eco template?


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.


Solution

  • 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 %>