A small Janet library to create HTML strings from Janet data structures. Similar to Clojure's Hiccup.
jpm install https://github.com/m7andrew/encode-html
Simple HTML structure:
(import html)
(html/encode
[:div {:class "foo"}
[:a {:href "/some/link"} "Link"]
[:span {} "Hello"]])
<div class="foo">
<a href="/some/link">Link</a>
<span>Hello</span>
</div>
Loop using html/each
:
(import html)
(def todos
["Write some janet code"
"Make a sandwich"
"Find the lost city of Atlantis"])
(html/encode
[:ul {}
(html/each item todos
[:li {} item])])
Loop using map
:
(import html)
(def todos
["Write some janet code"
"Make a sandwich"
"Find the lost city of Atlantis"])
(defn todo [item]
[:li {} item])
(html/encode
[:ul {} ;(map todo todos)])
Result using either method:
<ul>
<li>Write some janet code</li>
<li>Make a sandwich</li>
<li>Find the lost city of Atlantis</li>
</ul>
(html/encode element)
Takes an element and returns an HTML string.
(html/each x ds & body)
Like Janet's each. Returns the body for each x
in the data structure ds
.
Each element has the general form:
[tag attributes & children]
tag
is a keyword for the tag name.attributes
is a struct with keyword keys and string values.children
is a sequence of elements, strings, or other types of data.
Any child that is not another element (tuple) gets converted to a string. So you can do things like insert numbers directly:
[:span {} 42]
<span>42</span>