Skip to content

Commit

Permalink
Merge pull request #130 from phlex-ruby/kbs/fix-render-template
Browse files Browse the repository at this point in the history
Fix rendering explicit template in 1.1
  • Loading branch information
joeldrapper authored Nov 27, 2023
2 parents 2132576 + 0f43146 commit 4229e68
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/phlex/rails/sgml/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def render(*args, **kwargs, &block)
when Enumerable
return super unless renderable.is_a?(ActiveRecord::Relation)
else
@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
captured_block = -> { capture(&block) } if block
@_context.target << @_view_context.render(*args, **kwargs, &captured_block)
end

nil
Expand Down
3 changes: 3 additions & 0 deletions spec/internal/app/views/examples/sgml/_wrap.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Partial A
<%= yield %>
Partial B
1 change: 1 addition & 0 deletions spec/internal/app/views/examples/sgml/template.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Plain template.
51 changes: 51 additions & 0 deletions spec/phlex/sgml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Phlex::SGML do
describe "#render" do
context "when given a block" do
it "renders content in the proper order" do
component = Class.new(Phlex::HTML) do
def template(&block)
plain "Component A\n"
render("examples/sgml/wrap", &block)
plain "Component B\n"
end
end

content = ExamplesController.render(inline: <<~ERB, locals: { component: component })
ERB A
<%= render component.new do %>
Hello
<% end %>
ERB B
ERB

expect(content).to eq(<<~OUTPUT)
ERB A
Component A
Partial A
Hello
Partial B
Component B
ERB B
OUTPUT
end
end

it "renders a template without a block" do
component = Class.new(Phlex::HTML) do
def template
render(template: "examples/sgml/template")
end
end

content = ExamplesController.render(component)

expect(content).to eq("Plain template.")
end
end
end

0 comments on commit 4229e68

Please sign in to comment.