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

Support selective rendering #154

Merged
merged 7 commits into from
Mar 16, 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
8 changes: 8 additions & 0 deletions lib/phlex/rails/helper_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ def define_output_helper(method_name)
# frozen_string_literal: true

def #{method_name}(...)
context = @_context
return if context.fragments && !context.in_target_fragment

output = helpers.#{method_name}(...)

case output
Expand All @@ -24,6 +27,9 @@ def define_output_helper_with_capture_block(method_name)
# frozen_string_literal: true

def #{method_name}(*args, **kwargs, &block)
context = @_context
return if context.fragments && !context.in_target_fragment

output = if block
helpers.#{method_name}(*args, **kwargs) { capture(&block) }
else
Expand Down Expand Up @@ -69,6 +75,8 @@ def define_builder_yielding_helper(method_name, builder)
# frozen_string_literal: true

def #{method_name}(*args, **kwargs)
context = @_context
return if context.fragments && !context.in_target_fragment
output = if block_given?
helpers.#{method_name}(*args, **kwargs) { |form|
capture do
Expand Down
10 changes: 8 additions & 2 deletions lib/phlex/rails/sgml/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ def render(*args, **kwargs, &block)
end

def render_in(view_context, &block)
fragments = if (fragment_header = view_context.request.headers["X-Fragment"])
fragment_header.split(" ")
elsif (turbo_frame = view_context.request.headers["Turbo-Frame"])
[turbo_frame]
end

if block_given?
call(view_context: view_context) do |*args|
call(view_context: view_context, fragments: fragments) do |*args|
original_length = @_context.target.length

if args.length == 1 && Phlex::SGML === args[0] && !block.source_location&.[](0)&.end_with?(".rb")
Expand All @@ -53,7 +59,7 @@ def render_in(view_context, &block)
end
end.html_safe
else
call(view_context: view_context).html_safe
call(view_context: view_context, fragments: fragments).html_safe
end
end

Expand Down
2 changes: 1 addition & 1 deletion phlex-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
end
spec.require_paths = ["lib"]

spec.add_dependency "phlex", "~> 1.9"
spec.add_dependency "phlex", "~> 1.10"
spec.add_dependency "railties", ">= 6.1", "< 8"
spec.add_dependency "zeitwerk", "~> 2.6"

Expand Down
Loading