Add doc for content_for

This commit is contained in:
Sdogruyol 2016-07-10 13:00:13 +03:00
parent 51d6da5cc0
commit daac09a859

View file

@ -2,6 +2,35 @@ require "kilt"
CONTENT_FOR_BLOCKS = Hash(String, Proc(String)).new CONTENT_FOR_BLOCKS = Hash(String, Proc(String)).new
# <tt>content_for</tt> is a set of helpers that allows you to capture
# blocks inside views to be rendered later during the request. The most
# common use is to populate different parts of your layout from your view.
#
# The currently supported engines are: ecr and slang.
#
# == Usage
#
# You call +content_for+, generally from a view, to capture a block of markup
# giving it an identifier:
#
# # index.ecr
# <% content_for "some_key" do %>
# <chunk of="html">...</chunk>
# <% end %>
#
# Then, you call +yield_content+ with that identifier, generally from a
# layout, to render the captured block:
#
# # layout.ecr
# <%= yield_content "some_key" %>
#
# == And How Is This Useful?
#
# For example, some of your views might need a few javascript tags and
# stylesheets, but you don't want to force this files in all your pages.
# Then you can put <tt><%= yield_content :scripts_and_styles %></tt> on your
# layout, inside the <head> tag, and each view can call <tt>content_for</tt>
# setting the appropriate set of tags that should be added to the layout.
macro content_for(key) macro content_for(key)
CONTENT_FOR_BLOCKS[{{key}}] = ->() { CONTENT_FOR_BLOCKS[{{key}}] = ->() {
__kilt_io__ = MemoryIO.new __kilt_io__ = MemoryIO.new
@ -15,11 +44,6 @@ macro yield_content(key)
CONTENT_FOR_BLOCKS[{{key}}].call CONTENT_FOR_BLOCKS[{{key}}].call
end end
# Uses built-in ECR to render views.
# # Usage
# get '/' do
# render 'hello.ecr'
# end
macro render(filename, layout) macro render(filename, layout)
content = render {{filename}} content = render {{filename}}
render {{layout}} render {{layout}}