Django

Code

Ticket #3184 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

[patch] better unordered_list

Reported by: dummy@habmalnefrage.de Assigned to: adrian
Milestone: Component: Template system
Version: SVN Keywords:
Cc: simonbun@versea.be Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

If you have a var = ['item 1', 'item 2', 'item 3'] and do {{ var|unordered_list }} you will get an IndexError?:

IndexError? at ... string index out of range Request Method: GET Request URL: ... Exception Type: IndexError? Exception Value: string index out of range Exception Location: /usr/local/lib/python2.4/site-packages/django/template/defaultfilters.py in _helper, line 292

I would expect a simple result like this:

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

I made some patch for unordered_list and the regressiontest.

Attachments

defaultfilters.diff (1.1 kB) - added by dummy@habmalnefrage.de on 12/24/06 03:36:52.
better unordered_list
defaultfilters-test.diff (0.7 kB) - added by dummy@habmalnefrage.de on 12/24/06 03:37:21.
diff for regressiontest
defaultfilters-test.2.diff (1.2 kB) - added by dummy@habmalnefrage.de on 12/25/06 06:01:53.
new version, more common use case
defaultfilters-test.3.diff (1.2 kB) - added by dummy@habmalnefrage.de on 12/25/06 06:03:20.
new version, more common use case
defaultfilters.2.diff (1.9 kB) - added by dummy@habmalnefrage.de on 12/25/06 06:04:00.
new version, more common use case
defaultfilters.3.diff (1.6 kB) - added by dummy@habmalnefrage.de on 12/26/06 05:28:33.
hopefully my last version of the patch
defaultfilters.4.diff (1.6 kB) - added by dummy@habmalnefrage.de on 12/26/06 05:29:09.
hopefully my last version of the patch
defaultfilters-test.4.diff (1.7 kB) - added by dummy@habmalnefrage.de on 12/26/06 05:29:38.
hopefully my last version of the patch
better-unordered.diff (4.1 kB) - added by Dirk Datzert <dummy@habmalnefrage.de> on 01/26/07 13:23:17.
I made 1 whole patch, and added the failing testcase
better-unordered.patch (6.1 kB) - added by SmileyChris on 08/07/07 23:09:01.
completely rewritten with tests and docs

Change History

12/24/06 03:36:52 changed by dummy@habmalnefrage.de

  • attachment defaultfilters.diff added.

better unordered_list

12/24/06 03:37:21 changed by dummy@habmalnefrage.de

  • attachment defaultfilters-test.diff added.

diff for regressiontest

12/25/06 06:01:53 changed by dummy@habmalnefrage.de

  • attachment defaultfilters-test.2.diff added.

new version, more common use case

12/25/06 06:03:20 changed by dummy@habmalnefrage.de

  • attachment defaultfilters-test.3.diff added.

new version, more common use case

12/25/06 06:04:00 changed by dummy@habmalnefrage.de

  • attachment defaultfilters.2.diff added.

new version, more common use case

12/26/06 05:28:33 changed by dummy@habmalnefrage.de

  • attachment defaultfilters.3.diff added.

hopefully my last version of the patch

12/26/06 05:29:09 changed by dummy@habmalnefrage.de

  • attachment defaultfilters.4.diff added.

hopefully my last version of the patch

12/26/06 05:29:38 changed by dummy@habmalnefrage.de

  • attachment defaultfilters-test.4.diff added.

hopefully my last version of the patch

12/26/06 19:30:42 changed by SmileyChris

Good job on the patch! I always found using unordered_list seemed more difficult than it needed to be.

01/17/07 00:58:28 changed by simonbun <simonbun@versea.be>

  • cc set to simonbun@versea.be.

Hi, thanks for the patch.

I've mailed you about a small bug in the patch with arrays of 2+ depth. Test case:

['page1', ['page2', ['page3', ['page4']]]]

At first i tried patching your patch, but i ended up rewriting it because it was too complex for me.

I decided not to post my code as a diff seeing as it's not quite finished. The original filter inserted newlines and tabs so that the html was formatted nicely. Personally i have no need for this feature, so i didn't implement it.

It shouldn't be much work though, so if someone is still reading this, feel free to implement it.

code:

def unordered_list(value):
    
  def _recurse_children(parent):
    temp = ''
    
    for child in parent:
      temp += '<li>'
      
      if type(child) == ListType:
        temp += '<ul>' + _recurse_children(child) + '</ul>'
      else:
        temp += child
        
      temp += '</li>'
      
    return temp
      
  return _recurse_children(value)

Regards, Simon

(follow-up: ↓ 4 ) 01/17/07 01:36:58 changed by simonbun <simonbun@versea.be>

Hold on, child <ul>'s were not nested correctly. This is better:

def unordered_list(value):
    
  def _recurse_children(parent):
    temp = ''
    
    for child in parent:
      if type(child) == ListType:
        temp += '<ul>' + _recurse_children(child) + '</ul>'
      else:
        if temp != '':
          temp += '</li>'
        
        temp += '<li>' + child
      
    return temp
      
  return _recurse_children(value)

(in reply to: ↑ 3 ) 01/18/07 10:56:27 changed by dummy@habmalnefrage.de

Replying to simonbun <simonbun@versea.be>:

Hold on, child <ul>'s were not nested correctly. This is better: {{{ def unordered_list(value): def _recurse_children(parent): temp = for child in parent: if type(child) == ListType?: temp += '<ul>' + _recurse_children(child) + '</ul>' else: if temp != : temp += '</li>' temp += '<li>' + child return temp return _recurse_children(value) }}}

The code looks really much cleaner, but would break the current implementation results.

01/18/07 15:10:17 changed by SmileyChris

  • stage changed from Unreviewed to Ready for checkin.

01/25/07 11:01:33 changed by Gary Wilson <gary.wilson@gmail.com>

Being able to use this on a simple list will be nice.

01/25/07 11:13:25 changed by Gary Wilson <gary.wilson@gmail.com>

Also, the latest patch attached was before simonbun's comments about problems with lists nested more that 2 deep.

01/25/07 11:17:01 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch set to 1.
  • stage changed from Ready for checkin to Accepted.
  • needs_tests set to 1.
  • needs_docs set to 1.

Please provide a single patch including a test with simonbun's failing test case. We also might want to officially document that this works for a simple (unnested) list now.

01/26/07 13:23:17 changed by Dirk Datzert <dummy@habmalnefrage.de>

  • attachment better-unordered.diff added.

I made 1 whole patch, and added the failing testcase

08/07/07 23:09:01 changed by SmileyChris

  • attachment better-unordered.patch added.

completely rewritten with tests and docs

08/07/07 23:11:08 changed by SmileyChris

  • needs_better_patch deleted.
  • stage changed from Accepted to Ready for checkin.
  • needs_tests deleted.
  • needs_docs deleted.

08/25/07 20:11:20 changed by gwilson

  • status changed from new to closed.
  • resolution set to fixed.

(In [6019]) Fixed #3184 -- Changed the unordered_list template filter to use a more simple format, while maintaining backwards compatibility with the old format. unordered_list now works with a simple list of items. Thanks for the patch, SmileyChris?.


Add/Change #3184 ([patch] better unordered_list)




Change Properties
Action