First Commit
This commit is contained in:
55
src/web/helper/hb.ts
Normal file
55
src/web/helper/hb.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import * as Handlebars from "handlebars";
|
||||
import { readFileSync } from "fs";
|
||||
import config from "../../config";
|
||||
import Logging from "@hibas123/logging";
|
||||
|
||||
|
||||
function checkCondition(v1, operator, v2) {
|
||||
switch (operator) {
|
||||
case '==':
|
||||
return (v1 == v2);
|
||||
case '===':
|
||||
return (v1 === v2);
|
||||
case '!==':
|
||||
return (v1 !== v2);
|
||||
case '<':
|
||||
return (v1 < v2);
|
||||
case '<=':
|
||||
return (v1 <= v2);
|
||||
case '>':
|
||||
return (v1 > v2);
|
||||
case '>=':
|
||||
return (v1 >= v2);
|
||||
case '&&':
|
||||
return (v1 && v2);
|
||||
case '||':
|
||||
return (v1 || v2);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
|
||||
return checkCondition(v1, operator, v2)
|
||||
? options.fn(this)
|
||||
: options.inverse(this);
|
||||
});
|
||||
|
||||
|
||||
const formsTemplate = Handlebars.compile(readFileSync("./views/forms.hbs").toString());
|
||||
|
||||
const cache = new Map<string, Handlebars.TemplateDelegate>();
|
||||
|
||||
export default function getTemplate(name: string) {
|
||||
let tl: Handlebars.TemplateDelegate;
|
||||
if (!config.general.dev)
|
||||
tl = cache.get(name);
|
||||
|
||||
if (!tl) {
|
||||
Logging.debug("Recompiling template!");
|
||||
tl = Handlebars.compile(readFileSync(`./views/${name}.hbs`).toString());
|
||||
cache.set(name, tl);
|
||||
}
|
||||
|
||||
return tl;
|
||||
}
|
Reference in New Issue
Block a user