Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I suppress newlines after each template tag with Django's template engine?

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.

like image 591
ento Avatar asked Apr 09 '10 09:04

ento


People also ask

What is a more efficient way to pass variables from template to view in Django?

POST form (your current approach) This answer is perfect and I learned a great deal!

When {% extends %} is used for inheriting a template?

extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.

What does the built in Django template tag Lorem do?

lorem. Displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates.

Which template tag template variable is surrounded by?

A variable is a symbol within a template that outputs a value. Variable tags are surrounded by {{ and }} : My first name is {{ first_name }}. My last name is {{ last_name }}.


2 Answers

The closest I've found to what you're looking for (I'm looking for the same thing) is talk about a future patch, here: http://code.djangoproject.com/ticket/2594.

Unfortunately, it looks like there's not much you can do until they merge that patch in.

like image 200
CodeBlock Avatar answered Sep 19 '22 12:09

CodeBlock


{% spaceless %}
<ul>
    <li>apple</li>
    <li>banana</li>
    <li>cacao</li>
</ul>
{% endspaceless %}

I am not aware about any way to discard template tags lines. I'd vote for opening bug report.

like image 38
Almad Avatar answered Sep 20 '22 12:09

Almad