ehandlebars.create({ defaultLayout: 'app', helpers: { section: function(name, options) { if (!this._sections) this._sections = {} this._sections[name] = options.fn(this) return null } } })
router.post('/', (req, res, next) => { hbs.render('pdf_templates/example.hbs', req.body).then((renderedHtml) => { pdf.create(renderedHtml).toStream(function(err, stream) { stream.pipe(res); }); }); });
function initWebapp() { var express = require('express'); var exphbs = require('express-handlebars'); var path = require('path'); var app = express(); app.use("/static", express.static(path.join(__dirname, '/static'))); var filmsRoute = require('./routes/FilmRoute'); app.use('/films', filmsRoute); var hbs = exphbs.create({defaultLayout: 'main'}); app.engine('handlebars', hbs.engine); app.set('view engine', 'handlebars'); app.listen(3000); }
app.use(function (err, req, res, next){ console.log(err.stack); res.status(err.status || 500); res.render('error', { message: err.message, error: err, helpers: handlebars.helpers }); });
// TO DO configureTemplates() { const viewPath = resolve(__dirname, '..', 'app', 'views', 'emails'); // Configuring the way that the message will be formatted this.transporter.use( 'compile', // Defining the view engine configurations - Choosed: Handlebars nodemailerhbs({ viewEngine: exphbs.create({ layoutsDir: resolve(viewPath, 'layouts'), partialsDir: resolve(viewPath, 'partials'), defaultLayout: 'default', extname: '.hbs', }), viewPath, extName: '.hbs', }) ); }
middlewares() { this.app.use(bodyParser.urlencoded( {extended: true} )); this.app.use(bodyParser.json()); this.app.use(cookieParser()); this.app.use(session( { secret: 'secretpasscryp', resave: false, saveUninitialized: true, })); this.app.use(cors()) this.app.use(csrf({cookie: true})); this.app.use(validator()); this.app.engine('hbs', hbs({ extname: 'hbs', defaultLayout: 'layout', layoutsDir: 'views/layouts/', } )); this.app.set('view engine', 'hbs'); }
function Server() { // create an instance of express var app = express(); // add iframe replacement to express as middleware (adds res.merge method) app.use(iframeReplacement); // add handlebars view engine (you can use any) app.engine('hbs', exphbs()); // let express know how to locate the views/templates app.set('views', path.resolve(__dirname, 'views')); app.set('view engine', 'hbs'); // create simple route to test our fake news app.get('/', function(req, res) { // respond to this request with our fake-new content embedded within the BBC News home page res.merge('fake-news', { sourceUrl: 'http://www.bbc.co.uk/news', // external url to fetch sourcePlaceholder: 'div[data-entityid="container-top-stories#1"]' // css selector to inject our content into }); }); // start the server app.listen(8080, function() { console.log('Server running... Visit http://localhost:8080 in your browser'); }); }
expressHbs.create({ helpers: { inc: function (value, options) {
app.engine("handlebars", hbs({ partialsDir: path.join(__dirname, "views", "partials"), layoutsDir: path.join(__dirname, "views", "layouts"), defaultLayout: "main", helpers: { octicon: (i, options) => { if (!octicons[i]) { return new handlebars.SafeString(octicons.question.toSVG()); } return new handlebars.SafeString(octicons[i].toSVG()); }, eachpath: (path, options) => { if (typeof path != "string") { return ""; } let out = ""; path = path.split("/"); path.splice(path.length - 1, 1); path.unshift(""); path.forEach((folder, index) => { out += options.fn({ name: folder + "/", path: "/" + path.slice(1, index + 1).join("/"), current: index === path.length - 1 }); }); return out; }, } }));
app.use(validator()) app.use(express.static('public')) app.engine('handlebars', exphbs()) app.set('view engine', 'handlebars')
handlebars.create({ helpers: { __: function (value){
app.use(validator()) app.use(express.static('public')) app.engine('handlebars', exphbs()) app.set('view engine', 'handlebars')
exphbs.create({ defaultLayout: 'main', extname: '.hbs', helpers: { section: function(name, options){ if(!this._sections) this._sections = {}; this._sections[name] = options.fn(this); return null; } } })
// production error handler // no stacktraces leaked to user app.use(function (err, req, res, next){ console.log(err.stack); res.status(err.status || 500); res.render('error', { message: err.message, error: {}, helpers: handlebars.helpers }); });