Skip to content

Commit 360b4b2

Browse files
authored
Restore access to context in render? hook (#862)
1 parent d5401b5 commit 360b4b2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/phlex/sgml.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ def call(buffer = +"", context: {}, fragments: nil, &)
5656
end
5757

5858
def internal_call(parent: nil, state: nil, &block)
59-
return "" unless render?
60-
6159
if @_state
6260
raise Phlex::DoubleRenderError.new(
6361
"You can't render a #{self.class.name} more than once."
@@ -66,6 +64,8 @@ def internal_call(parent: nil, state: nil, &block)
6664

6765
@_state = state
6866

67+
return "" unless render?
68+
6969
block ||= @_content_block
7070

7171
Thread.current[:__phlex_component__] = [self, Fiber.current.object_id].freeze

quickdraw/sgml/conditional_rendering.test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ def view_template
1616
assert_equal_html Example.new(render: true).call, "<h1>Hello</h1>"
1717
assert_equal_html Example.new(render: false).call, ""
1818
end
19+
20+
class ExampleWithContext < Phlex::HTML
21+
def render? = context[:render]
22+
23+
def view_template
24+
h1 { "Hello" }
25+
end
26+
end
27+
28+
test do
29+
assert_equal_html ExampleWithContext.new.call(context: { render: true }), "<h1>Hello</h1>"
30+
assert_equal_html ExampleWithContext.new.call(context: { render: false }), ""
31+
end

0 commit comments

Comments
 (0)