Skip to content
Myr

[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

4 files+143 added−3 removed

modified:docs/src/index.txt+2 added-2 removed
28 unmodified lines
29 - `Debugging Support <debugging.txt>`_3031 - `Internationalization <i18n.txt>`_3233 - `Recipies <devrecipies.txt>`_33 - `Developer Recipies <devrecipies.txt>`_3435- Template Designer Documentation:3637 - `Syntax Reference <designerdoc.txt>`_3839 - `Differences To Django <fromdjango.txt>`_4041 - `Recipies <recipies.txt>`_41 - `Designer Recipies <recipies.txt>`_4243There is also support via IRC on the ``#pocoo`` channel on `irc.freenode.net`.
modified:jinja/filters.py+80 added-1 removed
616 unmodified lines
617 return value618 return wrapped619620621def 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:626627 .. sourcecode:: jinja628629 <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>638639 *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 wrapped659660661def 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:667668 .. sourcecode:: jinja669670 <table>671 {%- for row in items|batch(3, '&nbsp;') %}672 <tr>673 {%- for column in row %}674 <tr>{{ column }}</td>675 {%- endfor %}676 </tr>677 {%- endfor %}678 </table>679680 *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 wrapped696697698FILTERS = {699 'replace': do_replace,700 'upper': do_upper,701 'lower': do_lower,
29 unmodified lines
731 '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
1import jdebug2from jinja import from_string345template = 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>1819<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>3132<h1>Filled Table</h1>33<table>34 {%- for row in items|batch(3, '&nbsp;') %}35 <tr>36 {%- for column in row %}37 <td>{{ column }}</td>38 {%- endfor %}39 </tr>40 {%- endfor %}41</table>4243<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) }}">&nbsp;</td>52 {%- endif %}53 </tr>54 {%- endfor %}55</table>''')5657print template.render(items=range(16))
modified:CHANGES+4 added
33 unmodified lines
3435- fixed severe memcaching bug. Formerly it wasn't possible to use memcaching36 without enabling disk cache.3738- fixed a bug that allowed users to override the special names `_`, `true` etc.3940- added `batch` and `slice` filters414243Version 1.044-----------45(released Mar 23, 2007)46
1 unmodified line