Opened 7 years ago

Closed 5 years ago

#17251 closed defect (fixed)

[patch][cla] Enable looping over objects with {% for key, value in object %}

Reported by: hcampbell Owned by: dylans <dylan@…>
Priority: low Milestone: 1.11
Component: DojoX DTL Version: 1.9.0
Keywords: Cc:
Blocked By: Blocking:

Description

In the following example:

require([
    "dojo/dom-construct",
    "dojox/dtl/Context",
    "dojox/dtl/_base",
    "dojox/dtl/dom",
    "dojox/dtl/tag/logic"
    ],
    function(domConstruct, Context, dtl) {
        var templateString = "" +
                "<dl>" + 
                "    {% for k, v in obj %}" +
                "    <dt>{{ k }}</dt>" +
                "    <dd>{{ v }}</dd>" +
                "    {% endfor %}" +
                "</dl>",
            testObj = {
                "HTML": "HyperText Markup Language",
                "CSS": "Cascading Stylesheet"
            },
            template;
        
        template = new dtl.DomTemplate(templateString);
        template.render(new Context({"obj": testObj}));
        domConstruct.place(
            template.getRootNode(),
            document.body
        );
    }
);

The output is:

<dl>
       <dt>HyperText Markup Language</dt>
       <dd>undefined</dd>
       <dt>Cascading Stylesheet</dt>
       <dd>undefined</dd>
</dl>

ForNode? tests for objects and assigns values to the tag arguments. Ideally it would store both key and value, and zip to the supplied arguments, giving the expected result:

<dl>
       <dt>HTML</dt>
       <dd>HyperText Markup Language</dd>
       <dt>CSS</dt>
       <dd>Cascading Stylesheet</dd>
</dl>

The patch to fix the issue is very minor (see attached). This will, however, break existing templates that treats objects as array of values (ie discarding keys). This could be fixed by reversing the order of key/value pairs, but that would break compatibility with the Django template system.

Will reference pull request.

Change History (3)

comment:1 Changed 7 years ago by hcampbell

comment:2 Changed 5 years ago by dylan

Milestone: tbd1.11
Priority: undecidedlow
Summary: Enable looping over objects with {% for key, value in object %}[patch][cla] Enable looping over objects with {% for key, value in object %}

I'll land this for 1.11, and make a note in the release notes.

comment:3 Changed 5 years ago by dylans <dylan@…>

Owner: set to dylans <dylan@…>
Resolution: fixed
Status: newclosed

In 1dcf38ea5f15079355ac20df0b372753da9dd6bc/dojox:

Error: Processor CommitTicketReference failed
Unsupported version control system "git": Can't find an appropriate component, maybe the corresponding plugin was not enabled? 
Note: See TracTickets for help on using tickets.