Merge pull request #178 from sdogruyol/content-for

Content for
This commit is contained in:
Serdar Dogruyol 2016-07-09 19:59:49 +03:00 committed by GitHub
commit 51d6da5cc0
4 changed files with 34 additions and 0 deletions

View file

@ -0,0 +1,5 @@
Hello <%= name %>
<% content_for "custom" do %>
<h1>Hello from otherside</h1>
<% end %>

View file

@ -0,0 +1,3 @@
<html><%= content %></html>
<%= yield_content "custom" %>

View file

@ -34,4 +34,15 @@ describe "Views" do
client_response = call_request_on_app(request)
client_response.body.should contain("<html>Hello world")
end
it "renders layout with content_for" do
get "/view/:name" do |env|
name = env.params.url["name"]
render "spec/asset/hello_with_content_for.ecr", "spec/asset/layout_with_yield.ecr"
end
request = HTTP::Request.new("GET", "/view/world")
client_response = call_request_on_app(request)
client_response.body.should contain("<html>Hello world")
client_response.body.should contain("<h1>Hello from otherside</h1>")
end
end

View file

@ -1,5 +1,20 @@
require "kilt"
CONTENT_FOR_BLOCKS = Hash(String, Proc(String)).new
macro content_for(key)
CONTENT_FOR_BLOCKS[{{key}}] = ->() {
__kilt_io__ = MemoryIO.new
{{ yield }}
__kilt_io__.to_s
}
nil
end
macro yield_content(key)
CONTENT_FOR_BLOCKS[{{key}}].call
end
# Uses built-in ECR to render views.
# # Usage
# get '/' do