From 317d086b4c83c8677692db9799134799a22bcf31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Serdar=20Dogruyol=20-=20Sedo=20=E3=82=BB=E3=83=89?=
<990485+sdogruyol@users.noreply.github.com>
Date: Mon, 27 Jun 2022 12:28:13 +0300
Subject: [PATCH] fix content_for failing to capture the correct block input
(#639)
---
spec/asset/hello_with_content_for.ecr | 4 ++--
spec/asset/layout_with_yield.ecr | 4 +++-
spec/asset/layout_with_yield_and_vars.ecr | 8 +++++---
spec/view_spec.cr | 4 ++--
src/kemal/helpers/macros.cr | 9 ++++++++-
5 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/spec/asset/hello_with_content_for.ecr b/spec/asset/hello_with_content_for.ecr
index 149b294..b5460f9 100644
--- a/spec/asset/hello_with_content_for.ecr
+++ b/spec/asset/hello_with_content_for.ecr
@@ -1,5 +1,5 @@
Hello <%= name %>
-<% content_for "custom" do %>
-
Hello from otherside
+<% content_for "meta" do %>
+Kemal Spec
<% end %>
\ No newline at end of file
diff --git a/spec/asset/layout_with_yield.ecr b/spec/asset/layout_with_yield.ecr
index f6cd673..3710c4a 100644
--- a/spec/asset/layout_with_yield.ecr
+++ b/spec/asset/layout_with_yield.ecr
@@ -1,6 +1,8 @@
+
+ <%= yield_content "meta" %>
+
<%= content %>
- <%= yield_content "custom" %>
\ No newline at end of file
diff --git a/spec/asset/layout_with_yield_and_vars.ecr b/spec/asset/layout_with_yield_and_vars.ecr
index 3a82a7a..d2a8a35 100644
--- a/spec/asset/layout_with_yield_and_vars.ecr
+++ b/spec/asset/layout_with_yield_and_vars.ecr
@@ -1,8 +1,10 @@
+
+ <%= yield_content "meta" %>
+
<%= content %>
- <%= yield_content "custom" %>
- <%= var1 %>
- <%= var2 %>
+ <%= var1 %>
+ <%= var2 %>
\ No newline at end of file
diff --git a/spec/view_spec.cr b/spec/view_spec.cr
index 2a60574..79b8768 100644
--- a/spec/view_spec.cr
+++ b/spec/view_spec.cr
@@ -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("Hello from otherside
")
+ client_response.body.scan("Hello world").size.should eq(1)
+ client_response.body.should contain("Kemal Spec")
end
it "does not render content_for that was not yielded" do
diff --git a/src/kemal/helpers/macros.cr b/src/kemal/helpers/macros.cr
index 84982ff..71ba2be 100644
--- a/src/kemal/helpers/macros.cr
+++ b/src/kemal/helpers/macros.cr
@@ -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