2022-02-23 23:36:28 +00:00
|
|
|
CONTENT_FOR_BLOCKS = Hash(String, Tuple(String, Proc(Nil))).new
|
2016-07-09 16:54:35 +00:00
|
|
|
|
2017-03-03 20:42:08 +00:00
|
|
|
# `content_for` is a set of helpers that allows you to capture
|
2016-07-10 10:00:13 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2017-03-03 20:42:08 +00:00
|
|
|
# ## Usage
|
2016-07-10 10:00:13 +00:00
|
|
|
#
|
2017-03-03 20:42:08 +00:00
|
|
|
# You call `content_for`, generally from a view, to capture a block of markup
|
2016-07-10 10:00:13 +00:00
|
|
|
# giving it an identifier:
|
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
|
|
|
# # index.ecr
|
|
|
|
# <% content_for "some_key" do %>
|
|
|
|
# <chunk of="html">...</chunk>
|
|
|
|
# <% end %>
|
|
|
|
# ```
|
2016-07-10 10:00:13 +00:00
|
|
|
#
|
2017-03-03 20:42:08 +00:00
|
|
|
# Then, you call `yield_content` with that identifier, generally from a
|
2016-07-10 10:00:13 +00:00
|
|
|
# layout, to render the captured block:
|
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
|
|
|
# # layout.ecr
|
|
|
|
# <%= yield_content "some_key" %>
|
|
|
|
# ```
|
2016-07-10 10:00:13 +00:00
|
|
|
#
|
2017-03-03 20:42:08 +00:00
|
|
|
# ## And How Is This Useful?
|
2016-07-10 10:00:13 +00:00
|
|
|
#
|
|
|
|
# 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.
|
2017-03-03 20:42:08 +00:00
|
|
|
# Then you can put `<%= yield_content :scripts_and_styles %>` on your
|
|
|
|
# layout, inside the <head> tag, and each view can call `content_for`
|
2016-07-10 10:00:13 +00:00
|
|
|
# setting the appropriate set of tags that should be added to the layout.
|
2016-07-10 12:01:00 +00:00
|
|
|
macro content_for(key, file = __FILE__)
|
2022-02-23 23:36:28 +00:00
|
|
|
CONTENT_FOR_BLOCKS[{{key}}] = Tuple.new {{file}}, ->() { {{ yield }} }
|
2016-07-09 16:54:35 +00:00
|
|
|
nil
|
2016-07-09 15:57:35 +00:00
|
|
|
end
|
|
|
|
|
2017-03-03 20:42:08 +00:00
|
|
|
# Yields content for the given key if a `content_for` block exists for that key.
|
2016-07-09 16:54:35 +00:00
|
|
|
macro yield_content(key)
|
2016-07-10 12:01:00 +00:00
|
|
|
if CONTENT_FOR_BLOCKS.has_key?({{key}})
|
|
|
|
__caller_filename__ = CONTENT_FOR_BLOCKS[{{key}}][0]
|
2016-07-13 13:24:25 +00:00
|
|
|
%proc = CONTENT_FOR_BLOCKS[{{key}}][1]
|
2022-06-27 09:28:13 +00:00
|
|
|
|
|
|
|
if __content_filename__ == __caller_filename__
|
|
|
|
%old_content_io, content_io = content_io, IO::Memory.new
|
|
|
|
%proc.call
|
|
|
|
%result = content_io.to_s
|
|
|
|
content_io = %old_content_io
|
|
|
|
%result
|
|
|
|
end
|
2016-07-10 12:01:00 +00:00
|
|
|
end
|
2016-07-09 15:57:35 +00:00
|
|
|
end
|
|
|
|
|
2016-07-17 14:06:49 +00:00
|
|
|
# Render view with a layout as the superview.
|
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
|
|
|
# render "src/views/index.ecr", "src/views/layout.ecr"
|
|
|
|
# ```
|
2015-12-14 18:05:46 +00:00
|
|
|
macro render(filename, layout)
|
2016-07-10 12:01:00 +00:00
|
|
|
__content_filename__ = {{filename}}
|
2022-02-23 23:36:28 +00:00
|
|
|
content_io = IO::Memory.new
|
|
|
|
ECR.embed {{filename}}, content_io
|
|
|
|
content = content_io.to_s
|
|
|
|
layout_io = IO::Memory.new
|
|
|
|
ECR.embed {{layout}}, layout_io
|
|
|
|
layout_io.to_s
|
2015-12-14 18:05:46 +00:00
|
|
|
end
|
|
|
|
|
2016-07-17 14:06:49 +00:00
|
|
|
# Render view with the given filename.
|
|
|
|
macro render(filename)
|
2021-09-08 13:21:25 +00:00
|
|
|
ECR.render({{filename}})
|
2016-02-15 17:09:33 +00:00
|
|
|
end
|
|
|
|
|
2016-07-17 14:06:49 +00:00
|
|
|
# Halt execution with the current context.
|
|
|
|
# Returns 200 and an empty response by default.
|
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
|
|
|
# halt env, status_code: 403, response: "Forbidden"
|
|
|
|
# ```
|
2016-11-01 08:46:13 +00:00
|
|
|
macro halt(env, status_code = 200, response = "")
|
2016-03-29 21:48:58 +00:00
|
|
|
{{env}}.response.status_code = {{status_code}}
|
|
|
|
{{env}}.response.print {{response}}
|
2016-11-07 19:31:36 +00:00
|
|
|
{{env}}.response.close
|
2016-03-29 21:48:58 +00:00
|
|
|
next
|
|
|
|
end
|
2017-04-06 18:43:41 +00:00
|
|
|
|
|
|
|
# Extends context storage with user defined types.
|
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
2017-04-06 18:43:41 +00:00
|
|
|
# class User
|
|
|
|
# property name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# add_context_storage_type(User)
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
2017-04-06 18:43:41 +00:00
|
|
|
macro add_context_storage_type(type)
|
|
|
|
{{ HTTP::Server::Context::STORE_MAPPINGS.push(type) }}
|
|
|
|
end
|