Janet 1.39.1-e9c6678 Documentation
(Other Versions:
1.38.0
1.37.1
1.36.0
1.35.0
1.34.0
1.31.0
1.29.1
1.28.0
1.27.0
1.26.0
1.25.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.0
1.19.0
1.18.1
1.17.1
1.16.1
1.15.0
1.13.1
1.12.2
1.11.1
1.10.1
1.9.1
1.8.1
1.7.0
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
)
htmlgen
HTMLgen is a rendering engine that can render plain data structures into an HTML string. Its API has only one constant and two functions:
- constant
doctype
is a string with html5 doctype header - function
raw
returns the function that will add the raw string, passed as an argument to the function, to the output, when rendered, without any escaping. - function
html
has one required argumentdata
with the data structure rendered into bytes with HTML code. And optionalbuf
to which function renders final bytes. If you do not provide thebuf
, it will create a new one.
Rules for rendering data structures
Example:
(use spork/htmlgen)
(defn append-year [buf] (buffer/push buf (string ((os/date) :year))))
(html
@[[:head
@[[:meta {:charset "htf-8"}]
[:title "Spork"]]]
[:body
@[[:header "Menu"]
[:main [:section "News"]]
[:footer "All right reserved " append-year]]]])
=> @"<head><meta charset=\"htf-8\"/><title>Spork</title></head><body><header>Menu</header><main><section>News</section></main><footer>All right reserved 2022</footer></body>"
We will show how HTMLgen renders from the data structure by dissecting the example above:
- `array` (and
fiber
which is not in the example) each member of the sequence renders by one of these rules. tuple
represents the HTML tag. The first member must be the name of the HTML tag. The second member can be a dictionary with HTML attributes for the HTML tag. All the other members are children of the tag and renders according to these rules.string
,buffer
,number
andboolean
coerces to string and, if necessary, escaped and pushed to the buffer.function
gets the buffer, with which it can do whatever it wants to, presumably push some more content, but anything.nil
do not do anything to the buffer.
As you may see, the rules are straightforward, yet with the fiber
and function
types you have pretty endless possibilities when constructing the HTML code from data structures.
Reference
htmlgen/doctype-html htmlgen/escape htmlgen/html htmlgen/raw
htmlgen/html function source
(html data &opt buf) Render HTML from standard data structures. Fills the provided optional buffer, or new one if it is not provided, with the html bytes.
htmlgen/raw function source
(raw text) Get an object that can be used to splice in HTML literals. `text` is not escaped in the output string.