Skip to content

Add support for @first, @last and @index#10

Merged
vincent-psarga merged 2 commits intoSmartBear:masterfrom
schuetzm:implement-first-last
Jun 26, 2019
Merged

Add support for @first, @last and @index#10
vincent-psarga merged 2 commits intoSmartBear:masterfrom
schuetzm:implement-first-last

Conversation

@schuetzm
Copy link
Copy Markdown
Contributor

@schuetzm schuetzm commented Apr 5, 2019

Closes #2.

@vincent-psarga
Copy link
Copy Markdown
Contributor

Ok, it seems there's a slight issues with another PR that was merged previously (allowing non hash instances). So basically the merge! does not work anymore.

The idea I'm checking is:

  • saving the "this" value as previously
  • using your new methods to store @FIRST, @last and @index

If the each is used on a non-hash object, the special values won't be available, otherwise it works as you meant it.

Does it sound good for you ?

      register_helper('each') do |context, items, block, else_block|
        current_this = context.get('this')
        saved = context.save_special_variables

        if (items.nil? || items.empty?)
          if else_block
            result = else_block.fn(context)
          end
        else
          result = items.each_with_index.map do |item, index|
            context.add_item(:this, item)
            context.add_items :@index => index, :@first => (index == 0), :@last => (index == items.length-1)
            block.fn(context)
          end.join('')
        end

        context.add_item(:this, current_this)
        context.restore_special_variables saved
        result
      end

@vincent-psarga vincent-psarga merged commit 9a81b29 into SmartBear:master Jun 26, 2019
@vincent-psarga
Copy link
Copy Markdown
Contributor

That's merged (with some twist to make it a bit more generic for the things you've added).
Thank you for the contribution.

@schuetzm schuetzm deleted the implement-first-last branch June 27, 2019 17:28
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

Successfully merging this pull request may close these issues.

@first/@last within {{#each}}

2 participants