Skip to content

Commit

Permalink
DRY up the rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcsmith committed Nov 12, 2017
1 parent 3e313c8 commit 3c1af65
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/lucky_web/renderable.cr
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
module LuckyWeb::Renderable
macro render(page, **assigns)
view = {{ page }}.new(
{% for key, value in assigns %}
{{ key }}: {{ value }},
{% end %}
{% for key in EXPOSURES %}
{{ key }}: {{ key }},
macro render(page_class = nil, **assigns)
render_html_page(
{{ page_class || "#{@type.name}Page".id }},
{% if assigns.empty? %}
{} of String => String
{% else %}
{{ assigns }}
{% end %}
)
log_html_render(context, view)
body = view.render.to_s
LuckyWeb::Response.new(context, "text/html", body)
end

macro render(**assigns)
view = {{ "#{@type.name}Page".id }}.new(
macro render_html_page(page_class, assigns)
view = {{ page_class.id }}.new(
{% for key, value in assigns %}
{{ key }}: {{ value }},
{% end %}
Expand All @@ -27,7 +24,7 @@ module LuckyWeb::Renderable
LuckyWeb::Response.new(context, "text/html", body)
end

def log_html_render(context, view)
private def log_html_render(context, view)
context.add_debug_message("Rendered #{view.class.colorize(HTTP::Server::Context::DEBUG_COLOR)}")
end

Expand Down

0 comments on commit 3c1af65

Please sign in to comment.