Jonathan
Jonathan

Reputation: 2738

Django spaceless template tag advantages

I've been wondering about this a lot recently, what is exactly the purpose of Django's spaceless template tag?

Obviously it removes whitespaces from HTML tags, but other than that does it increase the speed at which the pages load?

What I mean to ask is, does removing whitespaces from html files help in any way?

Upvotes: 6

Views: 1725

Answers (2)

user1019261
user1019261

Reputation: 21

it would make sense to have a version of {%spaceless%} that cross boundaries (ala Ifs and Fors), bad sadly Django doesn't support this simple feature. I have a dynamic class that gets added by Me and there are dozens of lines in spaces (all spaces match up to the template). This shouldn't be too hard for the developers of Django to add. adding spaceless to each if would be extemely stupid.

if they are really worried, you could have a depth parameter as well.

Upvotes: 2

A newline/space is a character, yes, and it will increase file size especially with large loops and human readability optimized template files (like a large FK list).

It's also exceedingly easy and safe to throw in {% spaceless %} in a base template.

It also lets you keep template code indented for readability but properly spaceless for HTML rendering (I'm thinking commas are a common scenario).

Upvotes: 4

Related Questions