Skip to content

Commit

Permalink
Restore access to context in render? hook (phlex-ruby#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
willcosgrove authored Feb 15, 2025
1 parent d5401b5 commit 360b4b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def call(buffer = +"", context: {}, fragments: nil, &)
end

def internal_call(parent: nil, state: nil, &block)
return "" unless render?

if @_state
raise Phlex::DoubleRenderError.new(
"You can't render a #{self.class.name} more than once."
Expand All @@ -66,6 +64,8 @@ def internal_call(parent: nil, state: nil, &block)

@_state = state

return "" unless render?

block ||= @_content_block

Thread.current[:__phlex_component__] = [self, Fiber.current.object_id].freeze
Expand Down
13 changes: 13 additions & 0 deletions quickdraw/sgml/conditional_rendering.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,16 @@ def view_template
assert_equal_html Example.new(render: true).call, "<h1>Hello</h1>"
assert_equal_html Example.new(render: false).call, ""
end

class ExampleWithContext < Phlex::HTML
def render? = context[:render]

def view_template
h1 { "Hello" }
end
end

test do
assert_equal_html ExampleWithContext.new.call(context: { render: true }), "<h1>Hello</h1>"
assert_equal_html ExampleWithContext.new.call(context: { render: false }), ""
end

0 comments on commit 360b4b2

Please sign in to comment.