diff --git a/lib/ruby-handlebars/context.rb b/lib/ruby-handlebars/context.rb index 7abaf05..6dedd1f 100644 --- a/lib/ruby-handlebars/context.rb +++ b/lib/ruby-handlebars/context.rb @@ -88,8 +88,12 @@ def add_item(key, value) locals[key.to_sym] = value end - def add_items(hash) - hash.map { |k, v| add_item(k, v) } + def add_items(enumerable) + if enumerable.is_a?(Array) + enumerable.each_with_index { |v, k| add_item(k.to_s, v) } + else + enumerable.map { |k, v| add_item(k, v) } + end end def with_nested_context diff --git a/lib/ruby-handlebars/helpers/each_helper.rb b/lib/ruby-handlebars/helpers/each_helper.rb index 69cb5f9..43349b8 100644 --- a/lib/ruby-handlebars/helpers/each_helper.rb +++ b/lib/ruby-handlebars/helpers/each_helper.rb @@ -24,7 +24,7 @@ def self.apply_as(context, items, name, hash:, block:, else_block:, collapse:, * case items when Array items.each_with_index.map do |item, index| - add_and_execute(block, context, items, item, index, else_block, collapse, name => item) + add_and_execute(block, context, items, item, index, else_block, collapse, name => item, :@key => index.to_s) end.join('') when Hash items.each_with_index.map do |(key, value), index|