diff --git a/lib/phlex/context.rb b/lib/phlex/context.rb index 2c657487..211a0887 100644 --- a/lib/phlex/context.rb +++ b/lib/phlex/context.rb @@ -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] } diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index 4d686171..3983524c 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -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 @@ -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 @@ -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 @@ -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, &) }