Templates 2.0 (#636)
Templates 2.0 and a whole bunch of other refactoring
This commit is contained in:
@@ -40,6 +40,15 @@ export function handlebarHelpers() {
|
||||
nextWeek.setDate(nextWeek.getDate() + 7);
|
||||
return niceDate(nextWeek);
|
||||
},
|
||||
weekStart: (startOnMonday = true) => {
|
||||
const d = new Date();
|
||||
const day = d.getDay();
|
||||
let diff = d.getDate() - day;
|
||||
if (startOnMonday) {
|
||||
diff += day == 0 ? -6 : 1;
|
||||
}
|
||||
return niceDate(new Date(d.setDate(diff)));
|
||||
},
|
||||
ifEq: function (v1: any, v2: any, options: any) {
|
||||
if (v1 === v2) {
|
||||
return options.fn(this);
|
||||
|
||||
@@ -10,14 +10,22 @@ export function handlebarsSyscalls(): SysCallMapping {
|
||||
obj: any,
|
||||
globals: Record<string, any> = {},
|
||||
): string => {
|
||||
const templateFn = Handlebars.compile(
|
||||
template,
|
||||
{ noEscape: true },
|
||||
);
|
||||
return templateFn(obj, {
|
||||
helpers: handlebarHelpers(),
|
||||
data: globals,
|
||||
});
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user