Open ghost opened 5 years ago
The Handlebars "each" syntax is best for iterating key-value objects in an array. That way, every key comes from your data and has a name that you can find easy:
$table = [
[ 'a' => 'foo', 'b' => 'bar' ],
[ 'a' => 'baz', 'b' => 'quux' ],
];
{{#each table}}
A: {{a}}
B: {{b}}
{{/each}}
$table = [
[ 'key' => 'foo', 'value' => 'bar' ],
[ 'key' => 'baz', 'value' => 'quux' ],
];
{{#each table}}
KEY: {{key}}
VALUE: {{value}}
{{/each}}
It is also possible to access the whole current value, with the {{.}}
syntax.
And the current key index, via the {{@index}}
syntax.
See also http://handlebarsjs.com/builtin_helpers.html#iteration and https://zordius.github.io/HandlebarsCookbook/0019-each.html.
For example, your data can be displayed like this:
$data = array(
'table' => array(
'foo' => 'bar'
)
);
{{#each table}}
KEY: {{@index}}
VAL: {{.}}
{{/each}}
The PHP Code:
The Issue:
An array like ['key' => 'value'] cannot be cross by an each... It's work if you value is an array, but not a string...
Can you solve this ?