komponent: How access the given block inside the helper

I’m looking for using the given block to my component inside the helper. But I didn’t found how to retrieve the block.

I used block_given_to_component? and that’s works, but if I use @block_given_to_component.call but it returns me the content of my parent component.

Is it a missing feature? Or a mistake of usage?

I do some test by extended the ComponentHelper module like following:

module ComponentHelper
  def yield_or_property(property_name)
    if block_given_to_component?
      block_given_to_component
    elsif properties.key? property_name
      properties[property_name]
    else
      raise ArgumentError, "Missing required block component or parameter: #{property_name}"
    end
  end

  def property_or_yield(property_name)
    if properties.key? property_name
      properties[property_name]
    elsif block_given_to_component?
      block_given_to_component
    else
      raise ArgumentError, "Missing required block component or parameter: #{property_name}"
    end
  end

  private

  def block_given_to_component
    @_block_given_to_component = @block_given_to_component.yield if block_given_to_component?
  end
end

And by using this method yield_or_property(:text) into the components views.

But it returns me a LocalJumpError.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (17 by maintainers)

Most upvoted comments

Thanks @florentferry!

Looks like it works fine now!