Document helpers

This commit is contained in:
Sdogruyol 2016-07-17 17:06:49 +03:00
parent f7484d14d3
commit 725e051723
2 changed files with 16 additions and 5 deletions

View file

@ -15,9 +15,7 @@ def public_folder(path)
Kemal.config.public_folder = path Kemal.config.public_folder = path
end end
# Logs to output stream. # Logs to output stream. STDOUT is the default stream.
# development: STDOUT in
# production: kemal.log
def log(message) def log(message)
Kemal.config.logger.write "#{message}\n" Kemal.config.logger.write "#{message}\n"
end end
@ -27,15 +25,18 @@ def logging(status)
Kemal.config.logging = status Kemal.config.logging = status
end end
# Replaces Kemal::CommonLogHandler with a custom logger.
def logger(logger) def logger(logger)
Kemal.config.logger = logger Kemal.config.logger = logger
Kemal.config.add_handler logger Kemal.config.add_handler logger
end end
# Enables / Disables static file serving.
def serve_static(status) def serve_static(status)
Kemal.config.serve_static = status Kemal.config.serve_static = status
end end
# Helper for easily modifying response headers.
def headers(env, additional_headers) def headers(env, additional_headers)
env.response.headers.merge!(additional_headers) env.response.headers.merge!(additional_headers)
end end

View file

@ -42,6 +42,7 @@ macro content_for(key, file = __FILE__)
nil nil
end end
# Yields content for the given key if a content_for block exists for that key.
macro yield_content(key) macro yield_content(key)
if CONTENT_FOR_BLOCKS.has_key?({{key}}) if CONTENT_FOR_BLOCKS.has_key?({{key}})
__caller_filename__ = CONTENT_FOR_BLOCKS[{{key}}][0] __caller_filename__ = CONTENT_FOR_BLOCKS[{{key}}][0]
@ -50,16 +51,25 @@ macro yield_content(key)
end end
end end
# Render view with a layout as the superview.
#
# render "src/views/index.ecr", "src/views/layout.ecr"
#
macro render(filename, layout) macro render(filename, layout)
__content_filename__ = {{filename}} __content_filename__ = {{filename}}
content = render {{filename}} content = render {{filename}}
render {{layout}} render {{layout}}
end end
macro render(filename, *args) # Render view with the given filename.
Kilt.render({{filename}}, {{*args}}) macro render(filename)
Kilt.render({{filename}})
end end
# Halt execution with the current context.
# Returns 200 and an empty response by default.
#
# return_with env, status_code: 403, response: "Forbidden"
macro return_with(env, status_code = 200, response = "") macro return_with(env, status_code = 200, response = "")
{{env}}.response.status_code = {{status_code}} {{env}}.response.status_code = {{status_code}}
{{env}}.response.print {{response}} {{env}}.response.print {{response}}