Skip to content

Commit

Permalink
fix content_for failing to capture the correct block input (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdogruyol authored Jun 27, 2022
1 parent d53d253 commit 317d086
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions spec/asset/hello_with_content_for.ecr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Hello <%= name %>

<% content_for "custom" do %>
<h1>Hello from otherside</h1>
<% content_for "meta" do %>
<title>Kemal Spec</title>
<% end %>
4 changes: 3 additions & 1 deletion spec/asset/layout_with_yield.ecr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<html>
<head>
<%= yield_content "meta" %>
</head>
<body>
<%= content %>
<%= yield_content "custom" %>
</body>
</html>
8 changes: 5 additions & 3 deletions spec/asset/layout_with_yield_and_vars.ecr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<html>
<head>
<%= yield_content "meta" %>
</head>
<body>
<%= content %>
<%= yield_content "custom" %>
<%= var1 %>
<%= var2 %>
<%= var1 %>
<%= var2 %>
</body>
</html>
4 changes: 2 additions & 2 deletions spec/view_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe "Views" do
end
request = HTTP::Request.new("GET", "/view/world")
client_response = call_request_on_app(request)
client_response.body.should contain("Hello world")
client_response.body.should contain("<h1>Hello from otherside</h1>")
client_response.body.scan("Hello world").size.should eq(1)
client_response.body.should contain("<title>Kemal Spec</title>")
end

it "does not render content_for that was not yielded" do
Expand Down
9 changes: 8 additions & 1 deletion src/kemal/helpers/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ macro yield_content(key)
if CONTENT_FOR_BLOCKS.has_key?({{key}})
__caller_filename__ = CONTENT_FOR_BLOCKS[{{key}}][0]
%proc = CONTENT_FOR_BLOCKS[{{key}}][1]
%proc.call if __content_filename__ == __caller_filename__

if __content_filename__ == __caller_filename__
%old_content_io, content_io = content_io, IO::Memory.new
%proc.call
%result = content_io.to_s
content_io = %old_content_io
%result
end
end
end

Expand Down

0 comments on commit 317d086

Please sign in to comment.