digital-fabric/papercraft

Bootstrap extension example does not work

Closed this issue · 2 comments

Given an example file examples/extension_bootstrap.rb:

require 'bundler/setup'
require 'papercraft'

module BootstrapComponents
  def card(**props)
    div(class: 'card', **props) {
      div(class: 'card-body') {
        emit_yield
      }
    }
  end

  def card_title(title)
    h5 title, class: 'card-title'
  end
end

Papercraft.extension(bootstrap: BootstrapComponents)

page = Papercraft.html {
  bootstrap.card(style: 'width: 18rem') {
    bootstrap.card_title 'Card title'
    bootstrap.card_subtitle 'Card subtitle'
    bootstrap.card_text 'Some quick example text to build on the card title and make up the bulk of the card''s content.'
    bootstrap.card_link '#', 'Card link'
    bootstrap.card_link '#', 'Another link'
  }
}

puts page.render

When I try to run that file I get an error:

❯ ruby examples/extension_bootstrap.rb
Traceback (most recent call last):
	18: from examples/extension_bootstrap.rb:30:in `<main>'
	17: from /Users/some_user/code/papercraft/lib/papercraft/template.rb:121:in `render'
	16: from /Users/some_user/code/papercraft/lib/papercraft/template.rb:121:in `new'
	15: from /Users/some_user/code/papercraft/lib/papercraft/tags.rb:52:in `initialize'
	14: from /Users/some_user/code/papercraft/lib/papercraft/renderer.rb:88:in `initialize'
	13: from /Users/some_user/code/papercraft/lib/papercraft/renderer.rb:88:in `instance_eval'
	12: from /Users/some_user/code/papercraft/lib/papercraft/template.rb:123:in `block in render'
	11: from /Users/some_user/code/papercraft/lib/papercraft/template.rb:123:in `instance_exec'
	10: from examples/extension_bootstrap.rb:21:in `block in <main>'
	 9: from examples/extension_bootstrap.rb:6:in `card'
	 8: from /Users/some_user/code/papercraft/lib/papercraft/extension_proxy.rb:29:in `method_missing'
	 7: from /Users/some_user/code/papercraft/lib/papercraft/tags.rb:173:in `method_missing'
	 6: from /Users/some_user/code/papercraft/lib/papercraft/tags.rb:33:in `div'
	 5: from /Users/some_user/code/papercraft/lib/papercraft/tags.rb:33:in `instance_eval'
	 4: from examples/extension_bootstrap.rb:7:in `block in card'
	 3: from /Users/some_user/code/papercraft/lib/papercraft/tags.rb:33:in `div'
	 2: from /Users/some_user/code/papercraft/lib/papercraft/tags.rb:33:in `instance_eval'
	 1: from examples/extension_bootstrap.rb:8:in `block (2 levels) in card'
/Users/some_user/code/papercraft/lib/papercraft/renderer.rb:133:in `emit_yield': No block given (Papercraft::Error)

It seems the problem is related to passing the block around. I've tried with guarding with block_given? and passing the block explicitly but I could not make it work. Any ideas?

Yeah, the example is incorrect, the way to pass the block would be to do:

module BootstrapComponents
  def card(**props, &block)
    div(class: 'card', **props) {
      div(class: 'card-body', &block)
    }
  end
end

I added a test for this example, updated the README and made some minor improvements to the extension proxy on the way.

Thanks! That make it much more clear.