Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index not working inside a if #220

Closed
jbblanchet opened this issue Oct 21, 2013 · 7 comments
Closed

Index not working inside a if #220

jbblanchet opened this issue Oct 21, 2013 · 7 comments

Comments

@jbblanchet
Copy link

If I have a {{if}} inside a {{for}}, I don't have access to the index property: http://jsfiddle.net/bLbVg/

(I used the latest version for which I could find a cdn for jsfiddle, but I'm using commit 44 locally and it's not working either.)

Thanks.

@jbblanchet
Copy link
Author

By the way I'm aware that I can access the index through #parent.index, but it seems counterintuitive that index would refer to the {{if}} in that context, and not the {{for}}.

@BorisMoore
Copy link
Owner

Yes, this is a duplicate of the very last issue, at #219, and of several others (#96, #97, or #217). Take a look at them for responses.

(It's always best to look at existing issues before creating new ones...)

#foo is equivalent to #view.foo, and is a property on the current view. A view is an rendered template block. The current block you are in is the {{if}} block, so necessarily #index means something different in different blocks. But there are other ways to get properties from higher up in the hierarchy of views, as described in some of the other issues linked above...

Yet another one is

{{if ...}}
    {{:#getIndex()}}
{{/if}}

#getIndex() is available on any view and finds the index on the first parent view it finds on which an #indexproperty is defined...

@jbblanchet
Copy link
Author

Hi,

Thanks for the (very) quick response. I looked at open issues before posting, but I must admit I didn't search the closed ones. My bad, sorry for the duplicate. We've been using jsrender for over a year and we love it.

But the number of duplicates seems to indicate I'm not the only one expecting the index property to refer to the for. I understand that it refers to the current view context (that's why I used parent.index), but maybe you could consider making un-indexed views either inherit the index from their parent, or crawl back the tree to the nearest indexed view. It's just a suggestion, but as a user that's the behaviour I'd expect.

@BorisMoore
Copy link
Owner

Yes see my updated reply just above. That exists - it is #getIndex()...!

I agree a lot of folks hit this, but honestly I don't know how to avoid this issue, other than special casing #index as a syntax. I don't really want to create that kind of inconsistency...

@jbblanchet
Copy link
Author

Yeah, I left my reply open to answer the phone and replied to your original comment, not the updated one :).

@BorisMoore
Copy link
Owner

sure, np

@BorisMoore
Copy link
Owner

Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants