Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active January 13, 2017 20:40
Show Gist options
  • Save paulcsmith/5392b466ba88ede134175097d8f74812 to your computer and use it in GitHub Desktop.
Save paulcsmith/5392b466ba88ede134175097d8f74812 to your computer and use it in GitHub Desktop.
abstract class LuckyWeb::HTMLView
abstract def render
def initialize
@io = IO::Memory.new
end
EMPTY_HTML_ATTRS = {} of String => String
def h6(&block)
h6(EMPTY_HTML_ATTRS) do
yield
end
end
def h6(content, options)
h6(options) do
text content
end
end
def h6(content, **options)
h6(options) do
text content
end
end
def h6(content : String)
h6(EMPTY_HTML_ATTRS) do
text content
end
end
def h6(options, &block)
tag_attrs = build_tag_attrs(options)
@io << %(<h6#{tag_attrs}>)
yield
@io << %(</h6>)
end
def text(content : String)
@io << content
end
private def build_tag_attrs(options)
tag_attrs = String.build do |attrs|
options.each do |key, value|
attrs << %( #{key}="#{value}")
end
end
end
private def build_tag_attrs(**options)
tag_attrs = String.build do |attrs|
options.each do |key, value|
attrs << %( #{key}="#{value}")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment