Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow context to be set via call #821

Merged
merged 1 commit into from
Nov 25, 2024
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
5 changes: 3 additions & 2 deletions lib/phlex/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

# @api private
class Phlex::Context
def initialize(user_context = {})
def initialize(user_context: {}, view_context: nil)
@buffer = +""
@capturing = false
@user_context = user_context
@fragments = nil
@in_target_fragment = false
@halt_signal = nil
@view_context = view_context
end

attr_accessor :buffer, :capturing, :user_context, :in_target_fragment

attr_reader :fragments
attr_reader :fragments, :view_context

def target_fragments(fragments)
@fragments = fragments.to_h { |it| [it, true] }
Expand Down
17 changes: 9 additions & 8 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,16 @@ def to_proc
proc { |c| c.render(self) }
end

def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, fragments: nil, &block)
def call(buffer = +"", context: {}, view_context: nil, parent: nil, fragments: nil, &block)
@_buffer = buffer
@_context = context
@_view_context = view_context
@_context = phlex_context = parent&.__context__ || Phlex::Context.new(user_context: context, view_context:)
@_parent = parent

raise Phlex::DoubleRenderError.new("You can't render a #{self.class.name} more than once.") if @_rendered
@_rendered = true

if fragments
@_context.target_fragments(fragments)
phlex_context.target_fragments(fragments)
end

block ||= @_content_block
Expand All @@ -89,7 +88,7 @@ def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: n
Fiber[:__phlex_component__] = self
end

@_context.around_render do
phlex_context.around_render do
before_template(&block)

around_template do
Expand All @@ -113,10 +112,12 @@ def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: n
if Phlex::SUPPORTS_FIBER_STORAGE
Fiber[:__phlex_component__] = original_fiber_storage
end
buffer << context.buffer
buffer << phlex_context.buffer
end
end

protected def __context__ = @_context

def context
@_context.user_context
end
Expand Down Expand Up @@ -205,10 +206,10 @@ def flush
def render(renderable = nil, &)
case renderable
when Phlex::SGML
renderable.call(@_buffer, context: @_context, view_context: @_view_context, parent: self, &)
renderable.call(@_buffer, parent: self, &)
when Class
if renderable < Phlex::SGML
renderable.new.call(@_buffer, context: @_context, view_context: @_view_context, parent: self, &)
renderable.new.call(@_buffer, parent: self, &)
end
when Enumerable
renderable.each { |r| render(r, &) }
Expand Down
Loading