Skip to content

Commit

Permalink
Fix user context (#850)
Browse files Browse the repository at this point in the history
Closes #848
  • Loading branch information
joeldrapper authored Feb 10, 2025
1 parent 537b76d commit cb4e855
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def internal_call(parent: nil, state: nil, &block)
end

def context
@_state._state
@_state.user_context
end

# Output plain text.
Expand Down
29 changes: 29 additions & 0 deletions quickdraw/context.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

test "user context passed in from the outside" do
example = Class.new(Phlex::HTML) do
define_method :view_template do
h1 { context[:heading] }
end
end

context = { heading: "Hello, World!" }
assert_equal_html example.new.call(context:), %(<h1>Hello, World!</h1>)
end

test "user context passed down" do
a = Class.new(Phlex::HTML) do
define_method :view_template do
h1 { context[:heading] }
end
end

b = Class.new(Phlex::HTML) do
define_method :view_template do
context[:heading] = "Hello, World!"
render a.new
end
end

assert_equal_html b.new.call, %(<h1>Hello, World!</h1>)
end

0 comments on commit cb4e855

Please sign in to comment.