[svn] added `|batch` and `|slice` jinja filters
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent bf247fa · Browse files · 4 files·+143 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+143 added−3 removed
- tests:+57 added−0 removed
- other:+86 added−3 removed
Provenance
- Commit
d071f95- Tree
3e449a1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
bf247fa
4 files changed+143 added−3 removed
modified:docs/src/index.txt+2 added-2 removed
28 unmodified lines29 - `Debugging Support <debugging.txt>`_30 31 - `Internationalization <i18n.txt>`_32 33- - `Recipies <devrecipies.txt>`_33+ - `Developer Recipies <devrecipies.txt>`_34 35 - Template Designer Documentation:36 37 - `Syntax Reference <designerdoc.txt>`_38 39 - `Differences To Django <fromdjango.txt>`_40 41- - `Recipies <recipies.txt>`_41+ - `Designer Recipies <recipies.txt>`_42 43 There is also support via IRC on the ``#pocoo`` channel on `irc.freenode.net`.modified:jinja/filters.py+80 added-1 removed
616 unmodified lines617 return value618 return wrapped619 620 621+def do_slice(slices, fill_with=None):622+ """623+ Slice an iterator and return a list of lists containing624+ those items. Useful if you want to create a div containing625+ three div tags that represent columns:626+627+ .. sourcecode:: jinja628+629+ <div class="columwrapper">630+ {%- for column in items|slice(3) %}631+ <ul class="column-{{ loop.index }}">632+ {%- for item in column %}633+ <li>{{ item }}</li>634+ {%- endfor %}635+ </ul>636+ {%- endfor %}637+ </div>638+639+ *New in Jinja 1.1*640+ """641+ def wrapped(env, context, value):642+ result = []643+ seq = list(value)644+ length = len(seq)645+ items_per_slice = length // slices646+ slices_with_extra = length % slices647+ offset = 0648+ for slice_number in xrange(slices):649+ start = offset + slice_number * items_per_slice650+ if slice_number < slices_with_extra:651+ offset += 1652+ end = offset + (slice_number + 1) * items_per_slice653+ tmp = seq[start:end]654+ if fill_with is not None and slice_number >= slices_with_extra:655+ tmp.append(fill_with)656+ result.append(tmp)657+ return result658+ return wrapped659+660+661+def do_batch(linecount, fill_with=None):662+ """663+ A filter that batches items. It works pretty much like `slice`664+ just the other way round. It returns a list of lists with the665+ given number of items. If you provide a second parameter this666+ is used to fill missing items. See this example:667+668+ .. sourcecode:: jinja669+670+ <table>671+ {%- for row in items|batch(3, ' ') %}672+ <tr>673+ {%- for column in row %}674+ <tr>{{ column }}</td>675+ {%- endfor %}676+ </tr>677+ {%- endfor %}678+ </table>679+680+ *New in Jinja 1.1*681+ """682+ def wrapped(env, context, value):683+ result = []684+ tmp = []685+ for item in value:686+ if len(tmp) == linecount:687+ result.append(tmp)688+ tmp = []689+ tmp.append(item)690+ if tmp:691+ if fill_with is not None and len(tmp) < linecount:692+ tmp += [fill_with] * (linecount - len(tmp))693+ result.append(tmp)694+ return result695+ return wrapped696+697+698 FILTERS = {699 'replace': do_replace,700 'upper': do_upper,701 'lower': do_lower,29 unmodified lines731 'string': do_string,732 'urlize': do_urlize,733 'format': do_format,734 'capture': do_capture,658- 'trim': do_trim735+ 'trim': do_trim,736+ 'slice': do_slice,737+ 'batch': do_batch738 }added:tests/runtime/columntest.py+57 added
1+import jdebug2+from jinja import from_string3+4+5+template = from_string(u'''\6+<h1>Unfilled</h1>7+<div class="index">8+ {%- for column in items|slice(3) %}9+ <div class="col-{{ loop.index }}">10+ <ul>11+ {%- for item in column %}12+ <li>{{ item }}</li>13+ {%- endfor %}14+ </ul>15+ </div>16+ {%- endfor %}17+</div>18+19+<h1>Filled</h1>20+<div class="index">21+ {%- for column in items|slice(3, 'missing') %}22+ <div class="col-{{ loop.index }}">23+ <ul>24+ {%- for item in column %}25+ <li>{{ item }}</li>26+ {%- endfor %}27+ </ul>28+ </div>29+ {%- endfor %}30+</div>31+32+<h1>Filled Table</h1>33+<table>34+ {%- for row in items|batch(3, ' ') %}35+ <tr>36+ {%- for column in row %}37+ <td>{{ column }}</td>38+ {%- endfor %}39+ </tr>40+ {%- endfor %}41+</table>42+43+<h1>Unfilled Table</h1>44+<table>45+ {%- for row in items|batch(3) %}46+ <tr>47+ {%- for column in row %}48+ <td>{{ column }}</td>49+ {%- endfor %}50+ {%- if row|length < 3 %}51+ <td colspan="{{ 3 - (row|length) }}"> </td>52+ {%- endif %}53+ </tr>54+ {%- endfor %}55+</table>''')56+57+print template.render(items=range(16))modified:CHANGES+4 added
33 unmodified lines34 35 - fixed severe memcaching bug. Formerly it wasn't possible to use memcaching36 without enabling disk cache.37 38+- fixed a bug that allowed users to override the special names `_`, `true` etc.39 40+- added `batch` and `slice` filters41+42+43 Version 1.044 -----------45 (released Mar 23, 2007)46 1 unmodified line