{"action":"created","issue":{"url":"https://api.github.com/repos/linkedin/dustjs/issues/761","repository_url":"https://api.github.com/repos/linkedin/dustjs","labels_url":"https://api.github.com/repos/linkedin/dustjs/issues/761/labels{/name}","comments_url":"https://api.github.com/repos/linkedin/dustjs/issues/761/comments","events_url":"https://api.github.com/repos/linkedin/dustjs/issues/761/events","html_url":"https://github.com/linkedin/dustjs/issues/761","id":203879500,"number":761,"title":"Proposal for additional $ properties","user":{"login":"claviska","id":55639,"avatar_url":"https://avatars.githubusercontent.com/u/55639?v=3","gravatar_id":"","url":"https://api.github.com/users/claviska","html_url":"https://github.com/claviska","followers_url":"https://api.github.com/users/claviska/followers","following_url":"https://api.github.com/users/claviska/following{/other_user}","gists_url":"https://api.github.com/users/claviska/gists{/gist_id}","starred_url":"https://api.github.com/users/claviska/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/claviska/subscriptions","organizations_url":"https://api.github.com/users/claviska/orgs","repos_url":"https://api.github.com/users/claviska/repos","events_url":"https://api.github.com/users/claviska/events{/privacy}","received_events_url":"https://api.github.com/users/claviska/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-01-29T16:30:47Z","updated_at":"2017-02-01T00:28:06Z","closed_at":null,"body":"I don't want to submit a PR without some feedback about this first. Essentially, I'd like to add a handful of very useful $ properties.\r\n\r\nCurrently, we have `$idx` and `$len`. I'd like to add the following:\r\n\r\n- `$first` - equals `true` during the first iteration, `false` otherwise\r\n- `$last` - equals `true` during the last iteration, `false` otherwise\r\n- `$count` - equals the current count of the iteration (1-based index)\r\n\r\nI've come across a number of circumstances where the corresponding helpers are too verbose or don't work. Perhaps the biggest use case is being able to use the proposed properties with comparison helpers. Some examples:\r\n\r\n```dust\r\n{?$first}...{/$first}               <-- first item only\r\n{^$first}...{/$first}               <-- everything except the first item\r\n\r\n{?$last}...{/$last}                 <-- last item only\r\n{^$last}...{/$last}                 <-- everything except the last item\r\n\r\n{@eq key=$count value=4}{/eq}       <-- a specific item\r\n```\r\n\r\nI realize Dust aims to use \"less logic\", but sometimes we don't have complete control over datasets (or we have to go out of our way to adapt them). These special properties add a huge convenience to Dust templates.\r\n\r\nHere is a simple modification to [this section of the code](https://github.com/linkedin/dustjs/blob/master/lib/dust.js#L855-L867) that adds the propsed properties:\r\n\r\n```js\r\nif (len > 0) {\r\n  head = context.stack && context.stack.head || {};\r\n  head.$len = len;\r\n  for (i = 0; i < len; i++) {\r\n    head.$first = i === 0;\r\n    head.$last = i === len - 1;\r\n    head.$count = i + 1;\r\n    head.$idx = i;\r\n    chunk = body(chunk, context.push(elem[i], i, len));\r\n  }\r\n  head.$idx = undefined;\r\n  head.$len = undefined;\r\n  head.$first = undefined;\r\n  head.$last = undefined;\r\n  head.$count = undefined;\r\n  return chunk;\r\n} else if (skip) {\r\n  return skip(this, context);\r\n}\r\n```\r\n\r\nI haven't done extensive testing, but it's a pretty straight-forward addition that seems to work well. (Of course, it works properly in nested loops as well.)\r\n\r\nFWIW, I'd also suggest the aliases `$index` and `$length` for consistency and legibility, but that's another discussion.\r\n\r\nInterested to hear the developer's and the community's thoughts on this.\r\n\r\nRe:\r\n- #720\r\n- https://github.com/linkedin/dustjs-helpers/issues/15\r\n- https://github.com/linkedin/dustjs-helpers/issues/58\r\n- http://stackoverflow.com/questions/14781217/in-dust-js-can-the-inital-value-of-idx-not-be-zero-based"},"comment":{"url":"https://api.github.com/repos/linkedin/dustjs/issues/comments/276537886","html_url":"https://github.com/linkedin/dustjs/issues/761#issuecomment-276537886","issue_url":"https://api.github.com/repos/linkedin/dustjs/issues/761","id":276537886,"user":{"login":"claviska","id":55639,"avatar_url":"https://avatars.githubusercontent.com/u/55639?v=3","gravatar_id":"","url":"https://api.github.com/users/claviska","html_url":"https://github.com/claviska","followers_url":"https://api.github.com/users/claviska/followers","following_url":"https://api.github.com/users/claviska/following{/other_user}","gists_url":"https://api.github.com/users/claviska/gists{/gist_id}","starred_url":"https://api.github.com/users/claviska/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/claviska/subscriptions","organizations_url":"https://api.github.com/users/claviska/orgs","repos_url":"https://api.github.com/users/claviska/repos","events_url":"https://api.github.com/users/claviska/events{/privacy}","received_events_url":"https://api.github.com/users/claviska/received_events","type":"User","site_admin":false},"created_at":"2017-02-01T00:28:06Z","updated_at":"2017-02-01T00:28:06Z","body":"Awesome. I think these properties will be well-received and are fairly straightforward, but did you want to wait a bit for additional feedback before I submit a proper PR with tests?"}}
{"actor":{"display_login":"claviska"}}