yahoi
Version:
Yet Another Highly Opinionated Isomorphic Framework
39 lines (30 loc) • 850 B
JavaScript
import ControllerResponseType from './../ControllerResponseType';
import fs from 'fs';
import Handlebars from 'Handlebars';
export default class RenderView extends ControllerResponseType {
constructor(templateFile, data, props) {
super(props);
this.props = props;
this.data = data;
this.templateFile = templateFile;
}
render() {
return new Promise((fulfill, reject) => {
fs.readFile(`${this.templateFile}.handlebars`, 'utf8', (err, source) => {
if (err) {
reject(err)
} else {
let template = Handlebars.compile(source);
fulfill(template(this.data));
}
});
});
}
respond(req, res) {
this.render(req).then(content => {
res.send(content);
}).catch(e => {
res.send('error: ' + String(e));
});
}
}