@@ -157,11 +157,26 @@ def phlex_layout_handler(handler = Undefined)
157157 end
158158 end
159159
160+ # Retrieves or sets the Phlex context.
161+ # When no argument is provided, it returns the current Phlex context.
162+ #
163+ # @param context [Hash] The Phlex context to be set.
164+ # @return [Hash] The current Phlex context.
165+ def phlex_context ( context = Undefined )
166+ case context
167+ when Undefined
168+ opts . dig ( :phlex , :context )
169+ else
170+ opts [ :phlex ] [ :context ] = context
171+ end
172+ end
173+
160174 # Renders a Phlex object.
161175 # @param obj [Phlex::SGML] The Phlex object to be rendered.
176+ # @param context [Hash] The Phlex context to be used for rendering.
162177 # @param content_type [String, nil] The content type of the response.
163178 # @param stream [Boolean] Whether to stream the response or not.
164- def phlex ( obj , content_type : nil , stream : false )
179+ def phlex ( obj , context : phlex_context , content_type : nil , stream : false )
165180 raise TypeError . new ( obj ) unless obj . is_a? ( ::Phlex ::SGML )
166181
167182 content_type ||= "image/svg+xml" if obj . is_a? ( ::Phlex ::SVG )
@@ -176,10 +191,10 @@ def phlex(obj, content_type: nil, stream: false)
176191
177192 if stream
178193 self . stream do |out |
179- renderer . call ( out , view_context : self )
194+ renderer . call ( out , context : context , view_context : self )
180195 end
181196 else
182- renderer . call ( view_context : self )
197+ renderer . call ( context : context , view_context : self )
183198 end
184199 end
185200 end
0 commit comments