| Issue 23: | Trying to iterate over a string with repeated section should throw an error (JavaScript version) | |
| 1 person starred this issue and may be notified of changes. | Back to list |
Given the following template fragment
{.section term}
{.repeated section @}
<li>{@}</li>
{.or}
<li>{@}</li>
{.end}
{.end}
The following input works as expected:
{term: ['one', 'two', 'three'] }
The following input however does not:
{term: 'one'}
On Firefox the string is treated as an array and returns a list element for
each character. On Internet Explorer it throws an error. The expected
result would have been to call the {.or} section of the template and only
produce a single list element.
The problem occurs on line 276 of json-template.js (210 2009-04-30), where
the code assumes that if the object exists and has a length value greater
than zero it will repeat the section:
if (items && items.length > 0)
Changing this line to test for the presence of an array fixes the error and
produces the expected result. The resulting fix is as follows:
if (items && Object.prototype.toString.apply(items) === '[object Array]'
&& items.length > 0)
May 27, 2009
Project Member
#1
gtempacc...@yahoo.com
May 28, 2009
Ah, I interpreted the reference document slightly different and assumed the .or
section to be also executed if the intended target (a list) was not there. Perhaps
that was just wishful thinking.
The use case for this is automatically generated JSON from an XML source. I often get
the following XML:
<list>
<item>one</item>
<item>two</item>
</list>
The generated JSON then becomes:
{list: {item: ['one', 'two'] }}
Unfortunately, when the list has only one element, the resulting JSON is:
{list: {item: 'one'}}
I don't have much control (or rather, I don't want to) over the way the JSON is
generated as it is done automatically by an XSLT stylesheet from an XML source. I was
hoping to catch this case in the template.
May 31, 2009
I see. Yeah it's unforunate that you have that type of output and don't have control over it. But there are a lot of issues you can get with arbitrary JSON, and the general solution is to modify the data dictionary (automatically). I just checked in a Python example of this, in python/jsontemplate/datadict*.py. It should be simple to do a similar thing from JavaScript for your case, although possibly issue 22 is relevant too.
Jul 9, 2009
(No comment was entered for this change.)
Summary:
Trying to iterate over a string with repeated section should throw an error (JavaScript version)
Jul 24, 2009
b.l.stein, what did you end up doing? I have the same issue; my JSON is converted from XML, so sometimes a value is an array of strings, sometimes it is a string. I was thinking about converting certain strings to an array of a string (a singleton) manually, but it's not the nicest solution ...
Jul 27, 2009
I ended up writing formatter functions for some of my problem cases and rewriting the JSON in other cases (with a simple recursive function.)
Nov 7, 2009
(No comment was entered for this change.)
Labels:
Todo-JS
|