-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.ts
50 lines (47 loc) · 1.69 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* render.ts holds the page rendering functions for cold_ui.
*/
import { DEFAULT_HANDLEBARS_CONFIG, Handlebars } from "./hbs.ts";
const handle = new Handlebars(DEFAULT_HANDLEBARS_CONFIG);
/**
* renderPage takes a template path and a page object and returns a Response object.
*
* @param {string} template: this name of the template in the views folder
* @param {{Object} page_object: the page object to apply to template
* @returns {Promise<Response>} returns a response once everything is ready.
*/
export async function renderPage(
template: string,
page_object: { [k: string]: string | object | boolean | undefined },
): Promise<Response> {
let body: string = await handle.renderView(template, page_object);
if (body !== undefined) {
return new Response(body, {
status: 200,
headers: { "content-type": "text/html" },
});
}
body =
`<doctype html>\n<html lang="en">something went wrong, failed to render ${template}.</html>`;
return new Response(body, {
status: 501,
headers: { "content-type": "text/html" },
});
}
/**
* makePage takes a template path and a page object and returns a Response object.
*
* @param {string} template: this name of the template in the views folder
* @param {object} page_object: the page object to apply to template
* @returns {Promise<string>} returns a string once everything is ready.
*/
export async function makePage(
template: string,
page_object: { [k: string]: string | object },
): Promise<string> {
let body = await handle.renderView(template, page_object);
if (body !== undefined) {
return body;
}
return `<doctype html>\n<html lang="en">something went wrong, failed to render ${template}.</html>`;
}