Thanks for writing this code, but djangosnippets[1] is probably a
better place for it to go.  On this mailing list it'll get lost or
forgotten with all of the other posts.

[1] http://www.djangosnippets.org/

On Feb 16, 10:17 pm, Yosifov Pavel <[EMAIL PROTECTED]> wrote:
> May be this 2 tags (table generating with any "shape", flat index in
> nested loops) are helpful for somebody?
>
> Example of usage in django template:
>
> ===cut===
> {# List filials must exists in context! #}
> {% load table %}
> <table border="0" width="100%">
>     {% table filials "3x?" %}
>         {% for row in table_obj %}
>             <tr>
>             {% for record in row %}
>                 {% if record %}
>                     <td class="mf_table_cell"
> onclick="selcell('filial_{{forloop|flatindex}}')">
>                         <img src="/art/filial.gif" style="margin-
> bottom: 4px;"/><br/>
>                         <span id="filial_{{forloop|flatindex}}"
> class="mf_table_unselcell">{{ record }}</span>
>                     </td>
>                 {% else %}
>                     <td class="mf_table_cell">
>                         &nbsp;
>                     </td>
>                 {% endif %}
>             {% endfor %}
>             </tr>
>         {% endfor %}
>     {% endtable %}
> </table>
> ===cut===
>
> Python code:
>
> ===cut===
> from django import template
> from django.shortcuts import render_to_response
> from django.template.loader import get_template
>
> def reshape( lst, ncols, nrows, horiz=True ):
>     """Change shape of list lst (like APL, J, K, NumPy).
>     Return list of lists: ncols x nrows. horiz - is the
>     table direction (horizontal or vertical if horiz is
>     False). Empty cells will be with ''
>     """
>     if not ncols and not nrows: return lst
>
>     w = ncols or len(lst)/nrows + (1 if len(lst)%nrows else 0)
>     h = nrows or len(lst)/ncols + (1 if len(lst)%ncols else 0)
>
>     if horiz:
>         flatten = lambda irow, icol: icol + irow*w
>     else:
>         flatten = lambda irow, icol: irow + icol*h
>
>     rows = []
>     for irow in range( h ):
>         col = []
>         for icol in range( w ):
>             iflat = flatten( irow, icol )
>             el = '' if iflat >= len(lst) else lst[iflat]
>             col.append( el )
>         rows.append( col )
>
>     return rows
>
> register = template.Library()
>
> @register.tag
> def table( parser, token ):
>     """ Push in context 'table_obj' object - list of lists, result
>     of reshape function (see it)"""
>     try:
>         # lst - name of the list in the context, shape is string like
> 'H:4x?'|'V:2x2'|'?x3'
>         # 'H' - horiz direction, 'V' - vertical direction, '?' -
> computable size, digit -
>         # fixed size (see reshape function!)
>         tag, lst, shape = token.split_contents()
>     except ValueError:
>         sx = 'tag %r requires two arguments'%token.contents[0]
>         raise template.TemplateSyntaxError( sx )
>     shape = shape[1:-1].upper() # cut .. from ".."
>     if shape.startswith( 'V:' ):
>         horiz = False
>         shape = shape[2:]
>     elif shape.startswith( 'H:' ):
>         horiz = True
>         shape = shape[2:]
>     else:
>         horiz = True
>     w,h = shape.split( 'X' )
>     w = None if w == '?' else int(w)
>     h = None if h == '?' else int(h)
>     if not h and not w:
>         raise template.TemplateSyntaxError( 'only one axis may be
> unknown (?)' )
>
>     nodes = parser.parse( ('endtable',) )
>     parser.delete_first_token()
>     return TableNode( nodes, lst, w, h, horiz )
>
> class TableNode( template.Node ):
>     def __init__( self, nodes, lst, w, h, horiz ):
>         self.nodes = nodes
>         self.lst = lst
>         self.w = w
>         self.h = h
>         self.horiz = horiz
>     def render( self, context ):
>         # get list from context by it's name
>         res = reshape( context[self.lst], self.w, self.h, self.horiz )
>         context.push()
>         context[ 'table_obj' ] = res
>         txt = self.nodes.render( context )
>         context.pop()
>         return txt
>
> @register.filter
> def flatindex( forloop ):
>     """Existed in the nested loops return flat index
>     (index of flat list). For example:
>     like in C N-th dimension array may look like
>     flat array with ONE, flat index. This tag generate
>     this flat index for nested loops in django template (see nested {%
> for...%})"""
>     loop = forloop
>     level = 0
>     ret = 0
>     nestedlen = 1
>     while True:
>         if level == 0:
>             ret += loop['counter0']
>             # length of current level * size of previous levels is
>             # "weight" of ranking level. Ranking level add
>             # nestedlen to flatindex if move by 1 (like hypercube).
>             nestedlen *= loop['counter0'] + loop['revcounter0'] + 1
>         else:
>             ret += loop['counter0']*nestedlen
>
>         # go to next-up level
>         parentloop = loop.get('parentloop', None)
>         if parentloop:
>             loop = parentloop
>         else:
>             break
>         level += 1
>     return ret
> ===cut===
>
> Best regards
>   /Yosifov Pavel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to