From 9ade49ae20e511939fca9762925b14d64c47dbc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Kadir=20Ak=C4=B1n?= Date: Wed, 9 Dec 2015 17:52:56 +0200 Subject: [PATCH] Add layout support for render macro. It should be improved in the future. --- spec/asset/layout.ecr | 1 + spec/view_spec.cr | 10 ++++++++++ src/kemal/view.cr | 7 ++++++- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 spec/asset/layout.ecr diff --git a/spec/asset/layout.ecr b/spec/asset/layout.ecr new file mode 100644 index 0000000..d493b59 --- /dev/null +++ b/spec/asset/layout.ecr @@ -0,0 +1 @@ +<%= content %> diff --git a/spec/view_spec.cr b/spec/view_spec.cr index 0f38748..355ea3e 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -10,4 +10,14 @@ describe "Views" do response = kemal.call(request) response.body.should contain("Hello world") end + + it "renders layout" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/view/:name" do |env| + render "spec/asset/hello.ecr", "spec/asset/layout.ecr" + end + request = HTTP::Request.new("GET", "/view/world") + response = kemal.call(request) + response.body.should contain("Hello world") + end end diff --git a/src/kemal/view.cr b/src/kemal/view.cr index 2475675..4402f12 100644 --- a/src/kemal/view.cr +++ b/src/kemal/view.cr @@ -9,6 +9,11 @@ require "ecr/macros" macro render(filename) String.build do |__view__| - embed_ecr {{filename}}, "__view__" + embed_ecr({{filename}}, "__view__") end end + +macro render(filename, layout) + content = render {{filename}} + render {{layout}} +end