Nested arrays with {.repeated section}

30 views
Skip to first unread message

Andrey Fedorov

unread,
Nov 18, 2009, 1:38:20 PM11/18/09
to JSON Template
I have code which is saying something along the lines of:

<table>
{.repeated section cells}
<tr>
{.repeated section @}
<td class="cell">@</td>
{.end}
</tr>
{.end}
</table>

Running in the context {"cells":[[1,1,1],[1,1,1],[1,1,1]]}

The two @ are supposed to refer to different iterators. This code
seems to freeze up - I'm guessing it's because doing a repeated
section on @ breaks things. Is there a better (working?) way of
writing this?

Can't find this in the documentation or list history, sorry if it's
not a new question.

Cheers,
Andrey

Andy Chu

unread,
Nov 18, 2009, 11:13:15 PM11/18/09
to json-t...@googlegroups.com
Interesting, I thought this should work but indeed it doesn't. I see
the issue -- the way the context works, it's never changing @ to be
"1" -- only [1,1,1].

The simplest workaround is to make your JSON like:

{"cells": [
{"row": [1,1,1]},
{"row": [1,1,1]},
{"row": [1,1,1]},
]
}

I'll have to think about how/if this should work. It is kind of a
corner case. It definitely shouldn't cause an infinite loop though.

What is the exact use case here? Or did you just stumble on this?

thanks,
Andy

Andy Chu

unread,
Nov 18, 2009, 11:29:58 PM11/18/09
to json-t...@googlegroups.com
Nevermind, I figured it out. You know it's a bug when fixing it
*simplifies* the code rather than adding another case. Nice catch.

http://code.google.com/p/json-template/issues/detail?id=44

Which implementation were you using?

Andy




On Wed, Nov 18, 2009 at 10:38 AM, Andrey Fedorov <anfe...@gmail.com> wrote:
> --
>
> You received this message because you are subscribed to the Google Groups "JSON Template" group.
> To post to this group, send email to json-t...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
>
>
>

Oliver Oli

unread,
Nov 19, 2009, 8:17:30 AM11/19/09
to json-t...@googlegroups.com
I think I stumbled upon the same issue a few weeks ago, but thought
it's me not understanding jsontemplates. As I figured out the
workaround too, it was not a big issue anyway.

jsontemplate.py on google code was updated at the same time you fixed
the bug, but it's clearly different from _jsontemplate.py in the
mercurial repository.

Andrey Fedorov

unread,
Nov 19, 2009, 8:25:22 AM11/19/09
to json-t...@googlegroups.com
Cool, thanks. My use case is very similar to the example, just with
strings instead of numbers.

Was using the version linked from the main site:

http://json-template.googlecode.com/files/json-template.js

Should I check one out from SVN to get the fix?

- Andrey

Sent from my cell. Please forgive abbreviations and typos.
> .
>
>

Andy Chu

unread,
Nov 19, 2009, 11:28:22 AM11/19/09
to json-t...@googlegroups.com
On Thu, Nov 19, 2009 at 5:17 AM, Oliver Oli <olive...@gmail.com> wrote:
> I think I stumbled upon the same issue a few weeks ago, but thought
> it's me not understanding jsontemplates. As I figured out the
> workaround too, it was not a big issue anyway.
>
> jsontemplate.py on google code was updated at the same time you fixed
> the bug, but it's clearly different from _jsontemplate.py in  the
> mercurial repository.

Hm? I intended to make a direct copy, and it looks like it to me:

+D:\dev\hg\json-template\python\jsontemplate>md5sum jsontemplate.py
cb25fc84919458512213b5ddf65b6b2d *jsontemplate.py

+D:\dev\hg\json-template\python\jsontemplate>md5sum _jsontemplate.py
cb25fc84919458512213b5ddf65b6b2d *_jsontemplate.py

+D:\dev\hg\json-template\python\jsontemplate>diff jsontemplate.py
_jsontemplate.py

Did you pull?

Andy

Andy Chu

unread,
Nov 19, 2009, 11:28:47 AM11/19/09
to json-t...@googlegroups.com
On Thu, Nov 19, 2009 at 5:25 AM, Andrey Fedorov <anfe...@gmail.com> wrote:
> Cool, thanks. My use case is very similar to the example, just with
> strings instead of numbers.
>
> Was using the version linked from the main site:
>
> http://json-template.googlecode.com/files/json-template.js
>
> Should I check one out from SVN to get the fix?

The latest one I released should also have the fix. In the download
description, it shows the revision number it's copied from.

thanks,
Andy

Andrey Fedorov

unread,
Nov 19, 2009, 12:03:49 PM11/19/09
to json-t...@googlegroups.com
Yup, the latest one works great. Thanks!

There isn't any way to access the outer @, is there? For example, in tornado's templates, I'd do -

    <table>
        {% for i, n in enumerate(cells) %}
            <tr>
                {% for j, colr in enumerate(n) %}
                    <td class="cell" id="cell_{{i}}_by_{{j}}" style="background: {{colr}};"></td>
                {% end %}
           </tr>
       {% end %}
    </table>

- Andrey

--

You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-t...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/json-template?hl=.



Oliver Oli

unread,
Nov 19, 2009, 12:19:45 PM11/19/09
to json-t...@googlegroups.com
I downloaded it again from
http://code.google.com/p/json-template/downloads/list

it seems to be an older version

$ md5 jsontemplate.py
MD5 (jsontemplate.py) = daac178999e6cd19d7046b97139ff417

$ md5 _jsontemplate.py
MD5 (_jsontemplate.py) = cb25fc84919458512213b5ddf65b6b2d

Andy Chu

unread,
Nov 19, 2009, 12:47:30 PM11/19/09
to json-t...@googlegroups.com
On Thu, Nov 19, 2009 at 9:03 AM, Andrey Fedorov <anfe...@gmail.com> wrote:
> Yup, the latest one works great. Thanks!
> There isn't any way to access the outer @, is there? For example, in
> tornado's templates, I'd do -
>     <table>
>         {% for i, n in enumerate(cells) %}
>             <tr>
>                 {% for j, colr in enumerate(n) %}
>                     <td class="cell" id="cell_{{i}}_by_{{j}}"
> style="background: {{colr}};"></td>
>                 {% end %}
>            </tr>
>        {% end %}
>     </table>
> - Andrey

I think you're asking for @index there. @index will give you the
inner one. I don't think it's implemented but if you need it,
name.@index should give you an outer index.

It might have to be something like cells.@.@index, which is pretty
ugly. But generally to get a table, using an array of objects is more
conventional than a 2D array, which will avoid this ugliness.

Andy

Andy Chu

unread,
Nov 19, 2009, 12:47:50 PM11/19/09
to json-t...@googlegroups.com
On Thu, Nov 19, 2009 at 9:19 AM, Oliver Oli <olive...@gmail.com> wrote:
> I downloaded it again from
> http://code.google.com/p/json-template/downloads/list
>
> it seems to be an older version
>
> $ md5 jsontemplate.py
> MD5 (jsontemplate.py) = daac178999e6cd19d7046b97139ff417
>
> $ md5 _jsontemplate.py
> MD5 (_jsontemplate.py) = cb25fc84919458512213b5ddf65b6b2d

I think you didn't pull. The one in your workspace is the older one.

Andy
Reply all
Reply to author
Forward
0 new messages