Home Blog CV Projects Patterns Notes Book Colophon Search

Node Template Blocks

30 Nov, 2023

Some example block rendering with types:

/** @typedef {function(string): void} write - Write HTML */
// function write(text){
// }

/** 
* @typedef {function(write): void} Block - creates a new type named 'Block'
*/


/** @type {function(string): string} Escape HTML */
function escape(text){
    return text
}

/** @type {function(write, string, Block): void} Render a template */

const renderPage = (write, title, body) => {
	write(`<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>`)
	write(escape(title))
	write(`</title>
  <link rel="stylesheet" href="/static/styles.css?v=1.0">
  <script src="/static/htmx.min.js"></script>
</head>
<body>
  <p><a href="/messages">Messages</a></p>
`)
	if (body) {
		body(write)
	}
	write(`
</body>
</html>
`)
}


/** @type {function(write, string, Block, Block): void} Render a two column template */

const renderTwoColPage = (write, title, first, second) => {
	renderPage(write, title, (write) => {
		write(`<div id="first">`)
		first(write)
		write(`</div>\n<div id="second">`)
		second(write)
		write(`</div>`)
	})
}

Comments

Be the first to comment.

Add Comment





Copyright James Gardner 1996-2020 All Rights Reserved. Admin.