diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index fff20ab3..45040b22 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -60,30 +60,32 @@ def internal_call(parent: nil, state: nil, &block) block ||= @_content_block - previous_phlex_component = Thread.current[:__phlex_component__] - Thread.current[:__phlex_component__] = self - - state.around_render(self) do - before_template(&block) - - around_template do - if block - view_template do |*args| - if args.length > 0 - __yield_content_with_args__(*args, &block) - else - __yield_content__(&block) + begin + previous_phlex_component = Thread.current[:__phlex_component__] + Thread.current[:__phlex_component__] = self + + state.around_render(self) do + before_template(&block) + + around_template do + if block + view_template do |*args| + if args.length > 0 + __yield_content_with_args__(*args, &block) + else + __yield_content__(&block) + end end + else + view_template end - else - view_template end - end - after_template(&block) + after_template(&block) + end + ensure + Thread.current[:__phlex_component__] = previous_phlex_component end - ensure - Thread.current[:__phlex_component__] = previous_phlex_component end def context diff --git a/quickdraw/kit.test.rb b/quickdraw/kit.test.rb index 14c34503..5d796832 100644 --- a/quickdraw/kit.test.rb +++ b/quickdraw/kit.test.rb @@ -14,6 +14,32 @@ def view_template end end + class Components::Title < Phlex::HTML + def view_template + h1 { "Hello, world" } + end + + def render? + false + end + end + + class Components::Subtitle < Phlex::HTML + def view_template + h2 { "Welcome" } + end + end + + class Page < Phlex::HTML + include Components + + def view_template + Components::Title() + + Components::Subtitle() + end + end + test "raises when you try to render a component outside of a rendering context" do error = assert_raises(RuntimeError) { Components::SayHi("Joel") } assert_equal error.message, "You can't call `SayHi' outside of a Phlex rendering context." @@ -26,4 +52,9 @@ def view_template test "nested kits" do assert_equal phlex { Components::Foo::Bar() }, "

Bar

" end + + # Github issue: https://github.com/yippee-fun/phlex/issues/979 + test "phlex rendering context" do + assert_equal Page.call, %(

Welcome

) + end end