Closed boukeversteegh closed 10 years ago
would you please provide example(s) of templates that use this behavior?
On Fri, Nov 1, 2013 at 4:43 PM, Bouke Versteegh notifications@github.comwrote:
I've implemented @index https://github.com/index and @keyhttps://github.com/keysupport. A short description of how:
Handlebars_Context gets an index and key-stack. Handlebars_template->_variables() checks if the requested variable is @keyor @index, and returns the value from the context. Because it is a stack, accessing @index https://github.com/index in nested lists, and @keyhttps://github.com/keyin nested objects work without a problem.
Hope you could pull this request, because my main reason for using
Handlebars is actually the support for these variables.
You can merge this Pull Request by running
git pull https://github.com/boukeversteegh/handlebars.php master
Or view, comment on, or merge it at:
https://github.com/XaminProject/handlebars.php/pull/25 Commit Summary
- Added support for @index in sections: {{#listsection}}, and @key in objects: {{#each object}}
- Merge github.com:boukeversteegh/handlebars.php
File Changes
- M src/Handlebars/Context.phphttps://github.com/XaminProject/handlebars.php/pull/25/files#diff-0(72)
- M src/Handlebars/Helpers.phphttps://github.com/XaminProject/handlebars.php/pull/25/files#diff-1(4)
- M src/Handlebars/Template.phphttps://github.com/XaminProject/handlebars.php/pull/25/files#diff-2(14)
Patch Links:
regards, behrooz
Sure. It's part of the official Handlebars specification, see http://handlebarsjs.com/ (chapter: the each
block helper). You can use http://tryhandlebarsjs.com to try it out.
json
{
"dictionary": {
"hello": "ola",
"how are you?": "como estas?",
"thank you": "gracias"
},
"topwordlist": ["hello", "how are you?", "thank you"]
}
template
English - Spanish:
{{#each dictionary}}
- "{{@key}}" in Spanish is "{{this}}"
{{/each}}
Most Popular Words:
{{#topwordlist}}
#{{@index}} - {{this}}
{{/topwordlist}}
output
English - Spanish:
- "hello" in Spanish is "ola"
- "how are you?" in Spanish is "como estas?"
- "thank you" in Spanish is "gracias"
Most Popular Words:
#0 - hello
#1 - how are you?
#2 - thank you
One note. Handlebars also supports @index when #each listname
is used on a list, rather than the section syntax (#listname
). I haven't implemented that yet.
Thanks @boukeversteegh.
I've implemented @index and @key support. A short description of how:
Handlebars_Context
gets an index and key-stack.Handlebars_template->_variables()
checks if the requested variable is@key
or@index
, and returns the value from the context. Because it is a stack, accessing @index in nested lists, and @key in nested objects work without a problem.Hope you could pull this request, because my main reason for using Handlebars is actually the support for these variables.