Google Groups Home
Help | Sign in
linebreaksli template filter
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
  6 messages - Collapse all
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
rokclimb15@gmail.com  
View profile
 More options Jun 20 2007, 1:03 am
From: "rokclim...@gmail.com" <rokclim...@gmail.com>
Date: Wed, 20 Jun 2007 05:03:51 -0000
Local: Wed, Jun 20 2007 1:03 am
Subject: linebreaksli template filter
I don't want to attempt to pollute Django's beautiful template library
namespace with garbage, but I do see a legitimate value for a
linebreaksli template filter.  It does just what it says - converts a
string with linebreaks into a set of list items.  Sometimes, a <p> tag
just doesn't do the same thing, especially if you want to number the
items with an <ol>.  My code is below.  It wouldn't take too much to
convert it to a Django template filter.  What does everyone think?

def linebreaksli(value):
        "Converts strings with newlines into <li></li>s"
        value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
        lines = re.split('\n', value)
        lines = ['<li>%s</li>' % line for line in lines]
        return '\n'.join(lines)


    Reply to author    Forward  
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.
SmileyChris  
View profile
 More options Jun 20 2007, 1:30 am
From: SmileyChris <smileych...@gmail.com>
Date: Wed, 20 Jun 2007 05:30:03 -0000
Local: Wed, Jun 20 2007 1:30 am
Subject: Re: linebreaksli template filter
On Jun 20, 5:03 pm, "rokclim...@gmail.com" <rokclim...@gmail.com>
wrote:
>  My code is below.  It wouldn't take too much to
> convert it to a Django template filter.  What does everyone think?

Interesting filter.
Personally, I'm not sure this is useful enough for core.

    Reply to author    Forward  
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.
gulop...@gamemusic.org  
View profile
 More options Jun 20 2007, 8:52 am
From: gulop...@gamemusic.org
Date: Wed, 20 Jun 2007 08:52:25 -0400
Local: Wed, Jun 20 2007 8:52 am
Subject: Re: linebreaksli template filter
It looks like it'd make an excellent submission over at djangosnippets[1].

-Gul

[1] http://www.djangosnippets.org/

On 6/20/07, SmileyChris <smileych...@gmail.com> wrote:


    Reply to author    Forward  
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 Jun 21 2007, 8:04 pm
From: Gary Wilson <gary.wil...@gmail.com>
Date: Thu, 21 Jun 2007 19:04:56 -0500
Local: Thurs, Jun 21 2007 8:04 pm
Subject: Re: linebreaksli template filter

rokclim...@gmail.com wrote:
> I don't want to attempt to pollute Django's beautiful template library
> namespace with garbage, but I do see a legitimate value for a
> linebreaksli template filter.

A more general solution might be to have a linebreakstag filter that
takes arguments:

{{ mytext|linebreakstag:"<li>,</li>" }}

This tag would be able to handle what the linebreaksbr filter does too:

{{ mytext|linebreakstag:",<br />" }}

--

This has got me thinking though, do we even really need the two
linebreaks filters that exist now?

IMO, the linebreaks filter does too much when it wraps the string in
tags.  Those <p> tags should go in the template, not done by the filter.
    (Looking at tickets, this would fix #4560 [1].)  So, I think the
linebreaks filter should do what linkbreaksbr does, and linkbreaksbr
should go away.  Then, the new linebreaks could take arguments for the
start and end of each line.  (This would solve the OP's problem and also
#4573 [2].)  Something like:

text = "hello\nworld"

{{ text|linebreaks }} becomes:

hello<br />world

{{ text|linebreaks:"<li>,</li>" }} becomes:

<li>hello</li>
<li>world</li>

{{ text|linebreaks:",<hr />" }} becomes:

hello<hr />world

To get the old functionality of {{ text|linebreaks }}, you would have to
do <p>{{ text|linebreaks }}</p>.

Thoughts?

[1] http://code.djangoproject.com/ticket/4560
[2] http://code.djangoproject.com/ticket/4573


    Reply to author    Forward  
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 Jun 21 2007, 8:23 pm
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Fri, 22 Jun 2007 10:23:32 +1000
Local: Thurs, Jun 21 2007 8:23 pm
Subject: Re: linebreaksli template filter

On Thu, 2007-06-21 at 19:04 -0500, Gary Wilson wrote:

Hey .. you're back. :-) Hope you had a nice break.

> rokclim...@gmail.com wrote:
> > I don't want to attempt to pollute Django's beautiful template library
> > namespace with garbage, but I do see a legitimate value for a
> > linebreaksli template filter.

> A more general solution might be to have a linebreakstag filter that
> takes arguments:

> {{ mytext|linebreakstag:"<li>,</li>" }}

> This tag would be able to handle what the linebreaksbr filter does too:

> {{ mytext|linebreakstag:",<br />" }}

-1.

I don't like adding lots of customisation options to things like this.
It is *so* trivial to write a filter that does this if somebody really
wants that feature, but it isn't universally useful enough to warrant
the extra complexity. Turning line breaks into list items, for example,
is not a particularly semantically equivalent change.

> This has got me thinking though, do we even really need the two
> linebreaks filters that exist now?

> IMO, the linebreaks filter does too much when it wraps the string in
> tags.  Those <p> tags should go in the template, not done by the
> filter.
>     (Looking at tickets, this would fix #4560 [1].)  So, I think the
> linebreaks filter should do what linkbreaksbr does, and linkbreaksbr
> should go away.

People can just not use filters they don't like. They don't have to go
away. That's backwards-compatible.

Sure, there are a bunch of filters that show Django's history: they were
written to scratch some itch at Lawrence and made it into core as part
of open-sourcing the project. I don't really care whether they stay or
go, but would favour "stay" simply to avoid breaking existing templates.
However, that shouldn't be used as a doorway to introduce every possible
type of filter variation people might use once or twice. Instead, we
make it easy to add custom ones.

Regards,
Malcolm


    Reply to author    Forward  
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.
SmileyChris  
View profile
 More options Jun 21 2007, 8:44 pm
From: SmileyChris <smileych...@gmail.com>
Date: Fri, 22 Jun 2007 00:44:45 -0000
Local: Thurs, Jun 21 2007 8:44 pm
Subject: Re: linebreaksli template filter
> This has got me thinking though, do we even really need the two
> linebreaks filters that exist now?

Yes. You're underestimating what the linebreaks filter does.

It breaks the text up into a list, splitting on multiple new-lines.
Each of those list items get wrapped in a <p> (after getting their
<br />s)


    Reply to author    Forward  
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 »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google