commit
51d6da5cc0
4 changed files with 34 additions and 0 deletions
5
spec/asset/hello_with_content_for.ecr
Normal file
5
spec/asset/hello_with_content_for.ecr
Normal file
|
@ -0,0 +1,5 @@
|
|||
Hello <%= name %>
|
||||
|
||||
<% content_for "custom" do %>
|
||||
<h1>Hello from otherside</h1>
|
||||
<% end %>
|
3
spec/asset/layout_with_yield.ecr
Normal file
3
spec/asset/layout_with_yield.ecr
Normal file
|
@ -0,0 +1,3 @@
|
|||
<html><%= content %></html>
|
||||
|
||||
<%= yield_content "custom" %>
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue