Micro rendering template engine
Example
// Hapi.js example:
const Hapi = require('hapi');
const Vision = require('vision');
const HtmlFrmt = require('js-beautify').html;
const JsFrmt = require('js-beautify').js;
const Engine = require('templeo');
const econf = {
partialsURL: 'https://example.com', // partial reads from a server?
contextURL: 'https://example.com', // context read from a server?
partialsPath: 'views/partials', // file path to the partials
defaultExtension: 'html' // can be HTML, JSON, etc.
};
const cachier = new CachierFiles(econf, HtmlFrmt, JsFrmt);
const htmlEngine = new Engine(cachier);
// use the following instead if compiled templates don't need to be stored in files
// const htmlEngine = new Engine(econf, HtmlFrmt, JsFrmt);
const server = Hapi.Server({});
await server.register(Vision);
server.views({
compileMode: 'async',
relativeTo: '.',
path: 'views',
partialsPath: econf.partialsPath,
defaultExtension: econf.defaultExtension,
layoutPath: 'views/layout',
layout: true,
helpersPath: 'views/helpers',
engines: {
html: htmlEngine,
json: new JsonEngine()
}
});
// optionally set a partial function that can be accessed in the routes for
// instances where partials need to be generated, but not rendered to clients
server.app.htmlPartial = htmlEngine.genPartialFunc();
await server.start();
// it's a good practice to clear files after the server shuts down
server.events.on('stop', async () => {
await htmlEngine.clearCache();
});