Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
the template system's whitespace handling
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gary Wilson  
View profile  
 More options Aug 2 2006, 11:05 pm
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Wed, 02 Aug 2006 20:05:35 -0700
Local: Wed, Aug 2 2006 11:05 pm
Subject: the template system's whitespace handling

Breaking this discussion off from Strip Whitespace Middleware.

So, I think that the template system should not leave blank lines
behind if the tag(s) on that line evaulated to the empty string.  To
make sure we are all on the same page, here is an example:

This template code:
====================
<h1>My list</h1>

<ul>
{% for item in items %}
    <li>{{ item }}</li>
{% endfor %}
</ul>
====================

Would normally evaluate to:
====================
<h1>My list</h1>

<ul>

    <li>item 1</li>

    <li>item 2</li>

    <li>item 3</li>

</ul>
====================

And with the {% spaceless %} tag evaluates to:
====================
<h1>My list</h1> <ul> <li>item 1</li> <li>item 2</li> <li>item 3</li>
</ul>
====================

And with the StripWhitespace Middleware evaluates to
====================
<h1>My list</h1>
<ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
</ul>
====================

But what I think should evaluate to:
====================
<h1>My list</h1>

<ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
</ul>
====================

In other words, leave lines that I add, but remove the empty lines that
had template tags on them.

Gary Wilson


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Aug 3 2006, 12:41 am
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Thu, 03 Aug 2006 14:41:31 +1000
Local: Thurs, Aug 3 2006 12:41 am
Subject: Re: the template system's whitespace handling
On Wed, 2006-08-02 at 20:05 -0700, Gary Wilson wrote:

[...]

> In other words, leave lines that I add, but remove the empty lines that
> had template tags on them.

That was where I was coming from, too.

HTML output I don't care too much about (I mean, it's machine
interpreted, so beauty is a bit of a minor goal). For things like email
and text output, it becomes more relevant (and the spaceless tag is not
as handy in those cases).

Cheers,
Malcolm


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Aug 3 2006, 1:21 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Wed, 02 Aug 2006 22:21:25 -0700
Local: Thurs, Aug 3 2006 1:21 am
Subject: Re: the template system's whitespace handling

Malcolm Tredinnick wrote:
> That was where I was coming from, too.

Oh yeah, I meant to ask you more about the implementation you have.  Is
it functional?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Will McCutchen  
View profile  
 More options Aug 3 2006, 10:18 am
From: "Will McCutchen" <mccutc...@gmail.com>
Date: Thu, 03 Aug 2006 07:18:03 -0700
Local: Thurs, Aug 3 2006 10:18 am
Subject: Re: the template system's whitespace handling

Gary Wilson wrote:
> In other words, leave lines that I add, but remove the empty lines that
> had template tags on them.

+1

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Keating  
View profile  
 More options Aug 3 2006, 12:54 pm
From: "Tim Keating" <mrt...@gmail.com>
Date: Thu, 03 Aug 2006 16:54:42 -0000
Local: Thurs, Aug 3 2006 12:54 pm
Subject: Re: the template system's whitespace handling
This has been brought up a couple of times. See
http://code.djangoproject.com/ticket/696, which was marked wontfix,
reopened, and marked wontfix again. It's clearly not a concern the core
devs care about.

The solution seems to be, write your own middleware.

TK


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Bennett  
View profile  
 More options Aug 3 2006, 12:59 pm
From: "James Bennett" <ubernost...@gmail.com>
Date: Thu, 3 Aug 2006 11:59:18 -0500
Local: Thurs, Aug 3 2006 12:59 pm
Subject: Re: the template system's whitespace handling
On 8/3/06, Tim Keating <mrt...@gmail.com> wrote:

> This has been brought up a couple of times. See
> http://code.djangoproject.com/ticket/696, which was marked wontfix,
> reopened, and marked wontfix again. It's clearly not a concern the core
> devs care about.

Considering Malcolm is one of the "core devs", I'd be wary of your conclusion ;)

From looking at the ticket, the problem was not the general idea, but
rather the specific syntax proposed for it.

--
"May the forces of evil become confused on the way to your house."
  -- George Carlin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jeff@jeffcroft.com  
View profile  
 More options Aug 4 2006, 2:30 am
From: "j...@jeffcroft.com" <j...@jeffcroft.com>
Date: Thu, 03 Aug 2006 23:30:41 -0700
Local: Fri, Aug 4 2006 2:30 am
Subject: Re: the template system's whitespace handling
As someone who spends most of my day job working in Djangos' template
system, I can say that I would also prefer the same evaluation as Gary
99% of the time. I really think it should be the default.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Tobin  
View profile  
 More options Aug 4 2006, 11:48 am
From: "Tom Tobin" <korp...@gmail.com>
Date: Fri, 4 Aug 2006 10:48:04 -0500
Local: Fri, Aug 4 2006 11:48 am
Subject: Re: the template system's whitespace handling
On 8/4/06, j...@jeffcroft.com <j...@jeffcroft.com> wrote:

> As someone who spends most of my day job working in Djangos' template
> system, I can say that I would also prefer the same evaluation as Gary
> 99% of the time. I really think it should be the default.

I have to concur with Jeff; while my day job primarily involves models
and views, the default template behavior vis-a-vis whitespace and
template tags drives me fairly crazy in my personal projects.  ^_^  I
understand, theoretically speaking, the potential need for preserving
every last line of whitespace . . . but I'm having a hard time coming
up with a practical use case where Gary's proposed evaluation would
cause a problem.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ahmad Alhashemi  
View profile  
 More options Aug 4 2006, 1:58 pm
From: "Ahmad Alhashemi" <ahmad.alhash...@gmail.com>
Date: Fri, 04 Aug 2006 10:58:40 -0700
Local: Fri, Aug 4 2006 1:58 pm
Subject: Re: the template system's whitespace handling
In Rails, template tags can have an extra hyphen at the end to denote
the fact that they should consume the newline right after the tag. So:

{% some_tag %}

Would look like this:

{% some_tag -%}

Isn't it possible to just add this functionality by making the newline
that comes right after the tag part of the regex of the tag?

Something like this:

------------------------
# template syntax constants
[...]
BLOCK_TAG_END_NO_NEWLINE = '-%}\n'
[...]
tag_re = re.compile('(%s.*?(%s|%s)|%s.*?%s)' % \

(re.escape(BLOCK_TAG_START),
                                              re.escape(BLOCK_TAG_END),

re.escape(BLOCK_TAG_END_NO_NEWLINE),

re.escape(VARIABLE_TAG_START),

re.escape(VARIABLE_TAG_END)))
------------------------

Just one more small change to Lexer.create_token and we are all set up.
This way the newline will be matched with the tag and consumed, and
therefore it will just disappear. No big performance hit sustained.

(Note that I did not test any of this)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Tobin  
View profile  
 More options Aug 4 2006, 3:01 pm
From: "Tom Tobin" <korp...@gmail.com>
Date: Fri, 4 Aug 2006 14:01:57 -0500
Local: Fri, Aug 4 2006 3:01 pm
Subject: Re: the template system's whitespace handling
On 8/4/06, Ahmad Alhashemi <ahmad.alhash...@gmail.com> wrote:

> In Rails, template tags can have an extra hyphen at the end to denote
> the fact that they should consume the newline right after the tag. So:

> {% some_tag %}

> Would look like this:

> {% some_tag -%}

I can't help but feel that this syntax is ugly and, furthermore, easy
to miss while scanning through a template.  I wouldn't mind having to
set something once in my root-level template (e.g., {% chomp %}{%
endchomp %}) if it was picked up by child templates inheriting from
it.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Aug 4 2006, 7:19 pm
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Sat, 05 Aug 2006 09:19:28 +1000
Local: Fri, Aug 4 2006 7:19 pm
Subject: Re: the template system's whitespace handling

Not targeted at this particular response specifically (although it also
applies here), but more to head off a bunch of "here's a syntax change
we could make" replies...

Slightly tricky syntax changes was one of the problems with the original
ticket that was marked as "wontfix". It's complicating things too much
to add all this extra syntax. Nobody has come up with a good reason not
to consume lines that only consist of empty template tags and leading
whitespace, so we should just do the Right Thing always. Not
configurable, not optional, just do it. (There may be a reason; Anybody
who has concerns should feel free to sing out.)

General philosophy (eventually something like this will get written up
and placed in Django's documentation, along with a few other similar
points): It's very easy to go overboard when considering changes and try
to make everything configurable in order to please the mythical
"everybody". That has the effect of increasing the maintenance load (now
there are multiple code paths to maintain), increase the learning curve
(I'm just starting out with Django; why are there 200 configuration
options? Which ones do I care about?), increase the documentation load
and length (200 configuration options, again, that have to be kept up to
date) and increase the amount of knowledge a template designer needs (in
this case; remember that the template language is an *extra* thing a web
page developer needs to learn on top of all their other skills). Now
you've managed to make life harder for four groups of people with one
change designed to make things easier. Possibly not the intended
win-loss ratio.

For the sort of change Gary proposed, which is essentially identical to
what I and no doubt others had arrived at as well, there is no
meaningful semantic or syntactic difference for almost all cases. There
is a way to emulate the old (current) behaviour, so no functionality is
lost. PLus the new behaviour feels a bit more intuitive to many people,
it appears.

Back to the original problem:

My initial solution that I talked about the other day didn't work in a
few cases (ten minute hacks do that sometimes). I'll get back to this
today and see what we can come up with that is non-invasive, fast and
correct.

Best wishes,
Malcolm


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Keating  
View profile  
 More options Aug 11 2006, 11:39 am
From: "Tim Keating" <mrt...@gmail.com>
Date: Fri, 11 Aug 2006 15:39:09 -0000
Local: Fri, Aug 11 2006 11:39 am
Subject: Re: the template system's whitespace handling

I agree. It seems to me people either care about this or they don't.
Ergo the Django way to do this would be to make the whitespace handler
pluggable and set it globally in settings.py.

TK


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Aug 13 2006, 1:49 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Sat, 12 Aug 2006 22:49:16 -0700
Local: Sun, Aug 13 2006 1:49 am
Subject: Re: the template system's whitespace handling

Tim Keating wrote:
> I agree. It seems to me people either care about this or they don't.
> Ergo the Django way to do this would be to make the whitespace handler
> pluggable and set it globally in settings.py.

If performance does not suffer, I'm with Malcolm that it should simply
be done by default with no extra settings.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile  
 More options Aug 23 2006, 2:27 am
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Tue, 22 Aug 2006 23:27:22 -0700
Local: Wed, Aug 23 2006 2:27 am
Subject: Re: the template system's whitespace handling
I created a ticket:
http://code.djangoproject.com/ticket/2594

I also attached a patch that I have done a little testing with and
seems to work ok.  I first attacked this at the Node level, but
realized that might not be the best way because the Nodes get rendered
recursively.  In order to clean up the line's whitespace, you would
have to wait for everything to render and then remember which
lines/templatetags/templatevariables the render originated from.

The patch attacks the problem at the token level.  It finds the end of
lines and then evaluates each line to determine if whitespace should be
cleaned up.  But the patch is not perfect.  If you have lines in your
template like:
{% for item in items %}<li>{{ item.name }}</li>{% endfor %}
or
{% if item.name %}<li>{{ item.name }}</li>{% endif %}

where blocks are on the same line as text and the block renders no
output, my patch is fooled and still will insert a blank line in the
rendered string.

If, however, you were to instead write the above examples as:
{% for item in fooitems %}
<li>{{ item.name }}</li>
{% endfor %}
or
{% if item.name %}
<li>{{ item.name }}</li>
{% endif %}

then my patch will clean up the whitespace nicely.  This patch may also
very well not work on windows due to the different line endings.

There are a few setbacks though.  It seems that with this patch,
render_to_string becomes 2-3 times slower.  Toying with python's
profile for a few minutes shows that most of the time was being spent
in Token.__init__ and Node.__init__, which seemed logical due to all
the extra tokens, and hence nodes, that get created when using my patch.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »