From c14fb3de9669633e1224ae8133d64672cbe2a1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Kadir=20Ak=C4=B1n?= Date: Sun, 24 Jan 2016 19:13:10 +0200 Subject: [PATCH] Fix view rendering for Crystal 0.11.0 --- spec/asset/hello.ecr | 2 +- spec/view_spec.cr | 83 +++++++++++++++++++++++--------------------- src/kemal/macros.cr | 6 ++-- 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/spec/asset/hello.ecr b/spec/asset/hello.ecr index 355a5a2..1cc8d41 100644 --- a/spec/asset/hello.ecr +++ b/spec/asset/hello.ecr @@ -1 +1 @@ -Hello <%= env.params["name"] %> +Hello <%= name %> diff --git a/spec/view_spec.cr b/spec/view_spec.cr index 0794df9..23698e4 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -1,40 +1,43 @@ -# require "./spec_helper" -# -# macro render_with_base_and_layout(filename) -# render "spec/asset/#{{{filename}}}", "spec/asset/layout.ecr" -# end -# -# describe "Views" do -# it "renders file" do -# kemal = Kemal::Handler.new -# kemal.add_route "GET", "/view/:name" do |env| -# render "spec/asset/hello.ecr" -# end -# request = HTTP::Request.new("GET", "/view/world") -# io_with_context = create_request_and_return_io(kemal, request) -# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) -# client_response.body.should contain("Hello world") -# end -# -# it "renders file with dynamic variables" do -# kemal = Kemal::Handler.new -# kemal.add_route "GET", "/view/:name" do |env| -# render_with_base_and_layout "hello.ecr" -# end -# request = HTTP::Request.new("GET", "/view/world") -# io_with_context = create_request_and_return_io(kemal, request) -# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) -# client_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") -# io_with_context = create_request_and_return_io(kemal, request) -# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) -# client_response.body.should contain("Hello world") -# end -# end +require "./spec_helper" + +macro render_with_base_and_layout(filename) + render "spec/asset/#{{{filename}}}", "spec/asset/layout.ecr" +end + +describe "Views" do + it "renders file" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/view/:name" do |env| + name = env.params["name"] + render "spec/asset/hello.ecr" + end + request = HTTP::Request.new("GET", "/view/world") + io_with_context = create_request_and_return_io(kemal, request) + client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response.body.should contain("Hello world") + end + + it "renders file with dynamic variables" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/view/:name" do |env| + name = env.params["name"] + render_with_base_and_layout "hello.ecr" + end + request = HTTP::Request.new("GET", "/view/world") + io_with_context = create_request_and_return_io(kemal, request) + client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response.body.should contain("Hello world") + end + + it "renders layout" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/view/:name" do |env| + name = env.params["name"] + render "spec/asset/hello.ecr", "spec/asset/layout.ecr" + end + request = HTTP::Request.new("GET", "/view/world") + io_with_context = create_request_and_return_io(kemal, request) + client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response.body.should contain("Hello world") + end +end diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr index 1175633..8bd079e 100644 --- a/src/kemal/macros.cr +++ b/src/kemal/macros.cr @@ -6,9 +6,9 @@ require "ecr/macros" # render 'hello.ecr' # end macro render(filename) - String.build do |__view__| - embed_ecr({{filename}}, "__view__") - end + __view__ = String::Builder.new + embed_ecr({{filename}}, "__view__") + __view__.to_s end macro render(filename, layout)