UNPKG

tarkine

Version:

Tarkine - A lightweight and high-performance template engine for Node.js, designed for speed and simplicity.

51 lines (44 loc) 1.21 kB
const compiler = require("./compiler") const helpers = require("./helpers") const store = require("./store") function render(template, data = {}, includeCallback) { let func = helpers.caches.get(template) if (!func) { try { func = new Function( `$,__show,__loop,include,{${Object.keys(data)}}`, compiler.compile(template) ) helpers.caches.set(template, func) } catch (error) { throw new Error(`CompileError: ${error.message}\nSOURCE: CODE`) } } const include = typeof includeCallback === "function" ? includeCallback : () => { return '' } const output = func( store.getAll(), helpers.__show, helpers.__loop, include, data ) if (typeof output !== "string") { helpers.throwError( "RenderError", null, output.e, output.l, template ) } return output } module.exports = { render, store, ext: helpers.ext, compile: compiler.compile, resetCache: helpers.caches.clear, }