1
0
silverbullet/common/syscalls/handlebars.ts
Zef Hemel f30b1d3418
Templates 2.0 (#636)
Templates 2.0 and a whole bunch of other refactoring
2024-01-20 19:16:07 +01:00

32 lines
726 B
TypeScript

import { SysCallMapping } from "../../plugos/system.ts";
import { handlebarHelpers } from "./handlebar_helpers.ts";
import Handlebars from "handlebars";
export function handlebarsSyscalls(): SysCallMapping {
return {
"handlebars.renderTemplate": (
_ctx,
template: string,
obj: any,
globals: Record<string, any> = {},
): string => {
return renderHandlebarsTemplate(template, obj, globals);
},
};
}
export function renderHandlebarsTemplate(
template: string,
obj: any,
globals: Record<string, any>,
) {
const templateFn = Handlebars.compile(
template,
{ noEscape: true },
);
return templateFn(obj, {
helpers: handlebarHelpers(),
data: globals,
});
}