complate-stream
v0.16.10
Published
complate's core library for server-side rendering via JSX
Downloads
482
Readme
complate-stream
complate's core library for rendering HTML
How It Works
At the core of complate-stream lies generateHTML
, an implementation of the
signature expected by JSX (as
pioneered by React). When invoked,
that function returns an "element generator"; a function serving as placeholder
for the HTML element to be generated:
generateHTML("html", { lang: "en" },
generateHTML("body", { id: "top" },
generateHTML("h1", null, "hello world"),
generateHTML("p", null, "lorem ipsum")));
This indirection is necessary primarily to ensure proper order and nesting, as
function arguments are evaluated before the surrounding invocation – otherwise
the code above would emit <h1>
and <p>
before the <body>
and <html>
elements.
Thus the example above returns a hierarchy of element generators, encapsulated
by a single element generator at the root, which upon invocation writes HTML to
the stream
object being passed in:
let element = generateHTML(…);
element(stream, …);
(stream
is expected to support the methods #write
, #writeln
and #flush
;
see BufferedStream
for a sample implementation).
With our example, this should result in the following string being emitted:
<html lang="en"><body id="top"><h1>hello world</h1><p>lorem ipsum</p></body></html>