Last active
March 29, 2024 12:18
-
-
Save max-power/4076bffde40882b309ed22a352925bd7 to your computer and use it in GitHub Desktop.
Very simple HTML-Tags in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tag | |
def initialize(name, content = nil, **attributes, &block) | |
@name = name | |
@content = block_given? ? yield : content | |
@attributes = attributes | |
end | |
def to_s | |
"<#{@name}#{attributes}>#{@content}</#{@name}>" | |
end | |
private | |
def attributes | |
@attributes.map { |k,v| v.nil? ? k : %[#{k}="#{v}"] }.unshift('').join(' ') | |
end | |
end | |
#Tag.new(:ul, class: "power-list") do | |
# Tag.new(:li) do | |
# Tag.new(:a, href: "/help.html") { "Help" } | |
# end | |
# Tag.new(:li) do | |
# Tag.new(:a, "About", href: "/about.html") | |
# end | |
#end.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment