Skip to content

Instantly share code, notes, and snippets.

@pootsbook
Last active March 23, 2021 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pootsbook/0de09f2697e1378c9b9ef2d61fa40f97 to your computer and use it in GitHub Desktop.
Save pootsbook/0de09f2697e1378c9b9ef2d61fa40f97 to your computer and use it in GitHub Desktop.
require "hypertext"
require "hypertext/dsl"
class Hypertext
def append(fragment)
@dom << fragment
end
class DSL
def append(fragment)
@ht.append(fragment)
end
end
end
page_title = "Hello, World!"
def partial
Hypertext::DSL.new do
footer do
p do
text "Stop footering with the footer."
end
end
end
end
def content(page_title)
Hypertext::DSL.new do
div class: 'content' do
h1 do
text page_title
end
p do
text "Welcome to a page created in "
a href: "https://github.com/soveran/hypertext" do
text "Hypertext"
end
text "."
end
end
append partial
end
end
def layout(page_title, content)
Hypertext::DSL.new do
html lang: "en-GB" do
head do
title do
text page_title
end
end
body do
div class: 'container' do
append content
end
end
end
end.to_s
end
puts layout(page_title, content(page_title))
#=>
# <html lang="en-GB">
# <head>
# <title>
# Hello, World!
# </title>
# </head>
# <body>
# <div class="container">
# <div class="content">
# <h1>
# Hello, World!
# </h1>
# <p>
# Welcome to a page created in
# <a href="https://github.com/soveran/hypertext">
# Hypertext
# </a>
# .
# </p>
# </div>
# <footer>
# <p>
# Stop footering with the footer.
# </p>
# </footer>
#
#
# </div>
# </body>
# </html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment