Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#8041 closed bug (invalid)

jquery.tmpl plugin's tmplItem not returning expected item

Reported by: [email protected] Owned by: BorisMoore
Priority: low Milestone: 1.next
Component: templates Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:


If you render a template using an each and then access the dom item (for example in a live/delegate event handler) tmplItem returns the parent data structure as opposed to the desired item in the array:

    items: [
        {num: 1}
//instead of 
    num: 1

See http://jsfiddle.net/cKA3s/2/ Ideally, items inside of {{each}} should be considered nested templates

Change History (5)

comment:1 Changed 12 years ago by Rick Waldron

Component: unfiledtemplates
Owner: set to BorisMoore
Status: newassigned

comment:2 Changed 12 years ago by BorisMoore

Resolution: invalid
Status: assignedclosed

This is by design.

Under container2 there is only one rendered template instance. {{each}} does not create new nested template instances.

The feature you suggest is already available though, if you want it. Just replace {{each items}} by {{tmpl(items) "#tmpl3"}} with tmpl3 using the content you currently have within the {{each}} tag - <li>${ num }</li>.

{{tmpl}} is for template composition. {{each}} is for iteration within a template without creating sub-templates.

comment:3 Changed 12 years ago by [email protected]

Thanks for the note on tmpl. Unfortunately, tmpl does not have access to $index in the same way {{each}} does, or at least not in my testing. Is there a way to access index or would this have to be a plugin to the templating system to create a hybrid of the two tags?

comment:4 Changed 12 years ago by BorisMoore

There are a few ways you can work with index in nested templates. See https://github.com/jquery/jquery-tmpl/issues#issue/28 and http://bugs.jquery.com/ticket/7137 on reasons why it is not exposed, and some suggested approaches. Also status on whether that feature may later be added to jquery-templates.

comment:5 Changed 12 years ago by jitter

Priority: undecidedlow
Note: See TracTickets for help on using tickets.