Add support for @first, @last and @index#10
Merged
vincent-psarga merged 2 commits intoSmartBear:masterfrom Jun 26, 2019
schuetzm:implement-first-last
Merged
Add support for @first, @last and @index#10vincent-psarga merged 2 commits intoSmartBear:masterfrom schuetzm:implement-first-last
@first, @last and @index#10vincent-psarga merged 2 commits intoSmartBear:masterfrom
schuetzm:implement-first-last
Conversation
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: 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 |
Contributor
|
That's merged (with some twist to make it a bit more generic for the things you've added). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2.