views:

67

answers:

3

In Rails ERB, you can suppress newlines by adding a trailing hyphen to tags:

<ul>
  <% for @item in @items -%>
    <li><%= @item %></li>
  <% end -%>
</ul>

becomes:

<ul>
    <li>apple</li>
    <li>banana</li>
    <li>cacao</li>
</ul>

Is there a way to do this in Django? (Disclosure: I'm generating a csv file with Django)

Edit: Clarified that the newlines I'm hunting down are the ones left behind after the template tags.

A: 

When you write a template, you could open the ul hardcoded in the template and looping trought items en returning there value with the in it.

As far as i know there isn't a default function for that.

Bloeper
A: 

For example:

<ul>
{% for obj in list %}
    <li>{{ obj|linebreaksbr|striptags }}</li>
{% endfor %}
</ul>

Or this, which also strips whitespaces/tabs/etc, between HTML tags:

{{ spaceless }}
<ul>
{% for obj in list %}
    <li>{{ obj }}</li>
{% endfor %}
</ul>
{{ endspaceless }}
jweyrich
+1  A: 
{% spaceless %}
<ul>
    <li>apple</li>
    <li>banana</li>
    <li>cacao</li>
</ul>
{% endspaceless %}
Almad