Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions quickdraw/kit.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -26,4 +52,9 @@ def view_template
test "nested kits" do
assert_equal phlex { Components::Foo::Bar() }, "<h1>Bar</h1>"
end

# Github issue: https://github.com/yippee-fun/phlex/issues/979
test "phlex rendering context" do
assert_equal Page.call, %(<h2>Welcome</h2>)
end
end
Loading