Opened 18 years ago
Closed 17 years ago
#3184 closed defect (fixed)
[patch] better unordered_list
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | simonbun@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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 (10)
Change History (20)
by , 18 years ago
Attachment: | defaultfilters.diff added |
---|
by , 18 years ago
Attachment: | defaultfilters-test.4.diff added |
---|
hopefully my last version of the patch
comment:1 by , 18 years ago
Good job on the patch! I always found using unordered_list
seemed more difficult than it needed to be.
comment:2 by , 18 years ago
Cc: | added |
---|
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 comment:3 by , 18 years ago
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)
comment:4 by , 18 years ago
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.
comment:5 by , 18 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:7 by , 18 years ago
Also, the latest patch attached was before simonbun's comments about problems with lists nested more that 2 deep.
comment:8 by , 18 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Ready for checkin → Accepted |
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.
by , 18 years ago
Attachment: | better-unordered.diff added |
---|
I made 1 whole patch, and added the failing testcase
by , 17 years ago
Attachment: | better-unordered.patch added |
---|
completely rewritten with tests and docs
comment:9 by , 17 years ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
Triage Stage: | Accepted → Ready for checkin |
comment:10 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
better unordered_list