#NEWLINE# def do_batch( value , linecount , fill_with = None ) : #NEWLINE# #INDENT# "\n    A filter that batches items. It works pretty much like `slice`\n    just the other way round. It returns a list of lists with the\n    given number of items. If you provide a second parameter this\n    is used to fill up missing items. See this example:\n\n    .. sourcecode:: html+jinja\n\n        <table>\n        {%- for row in items|batch(3, '&nbsp;') %}\n          <tr>\n          {%- for column in row %}\n            <td>{{ column }}</td>\n          {%- endfor %}\n          </tr>\n        {%- endfor %}\n        </table>\n    " #NEWLINE# tmp = [ ] #NEWLINE# for item in value : #NEWLINE# #INDENT# if ( len ( tmp ) == linecount ) : #NEWLINE# #INDENT# ( yield tmp ) #NEWLINE# tmp = [ ] #NEWLINE# #UNINDENT# tmp . append ( item ) #NEWLINE# #UNINDENT# if tmp : #NEWLINE# #INDENT# if ( ( fill_with is not None ) and ( len ( tmp ) < linecount ) ) : #NEWLINE# #INDENT# tmp += ( [ fill_with ] * ( linecount - len ( tmp ) ) ) #NEWLINE# #UNINDENT# ( yield tmp )