Skip to content

Commit 48f48f3

Browse files
committed
Support Phlex component context
1 parent 1f45958 commit 48f48f3

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
88

99
### Added
1010

11+
- Support for Phlex 2.0 Component context. (`HelloWorld.new.call(context: {some: :data})`).
1112
- `:delegate_on` plugin option to specify the object to delegate methods to.
1213
Defaults to `::Phlex::SGML` but its advised to set it to your own subclass of `::Phlex::SGML`.
1314
- `:delegate_name` plugin option to specify name of the method that delegates to the Roda app.

lib/roda/plugins/phlex.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)