ralyodio / express-template-demo Goto Github PK
View Code? Open in Web Editor NEWDemo of using template engines with express.js and node.js
Demo of using template engines with express.js and node.js
Hey,
You can find code for integrating it here: http://nunjucks.jlongster.com/home#Express
It basically looks like this:
var nunjucks = require('nunjucks');
var express = require('express');
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'));
env.express(app);
You pass the path of your templates to the FileSystemLoader. You can ignore all the app.set("views")
crap and other settings.
Example is working fine for version 2.2.4 of dustjs-linkedin.
However with the current 2.3.0 this error appears when rendering a page:
Error: Template name parameter cannot be undefined when calling dust.compile
Running npm list:
├── [email protected]
├── [email protected]
├── [email protected]
[..]
├─┬ [email protected]
[..]
$ node -v
v0.10.24
500 Error: ENOENT, open '/Users/macbook/Downloads/express-template-demo/demo/views/views/layout.dust'
var consolidate = require('consolidate'),
swig = require('swig');
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.engine('.html', consolidate.swig);
app.get('/test', function(req, res) {
res.render('two');
});
/* Tell swig where to look for templates when one extends another. */
swig.init({ root: __dirname + '/views' });
Could you change Line 55 in demo/app
to
app.use(require('less-middleware')(path.join(__dirname, 'public')));
This conforms to the new less middleware syntax
I don't know why but my computer (OS: Ubuntu 20.04 LTS) is hanging in the middle of npm install
. I had to power-button-force-shutdown twice by now.
I would like to use DustJS in an expressJS application but I don't know how to add custom helpers to use in my templates.
From the DustJS docs I guess I need to implement code like this:
dust.helpers.myHelper = function(chunk, context, bodies, params) {
code of the helper
}
My problem is when to apply this code.
I've read the consolidate.js code and found that they are caching dustJS module internally, so the helpers I add in my application are not available in the templates.
Code form consolidate:
exports.dust.render = function(str, options, fn){
var engine = requires.dust;
if (!engine) {
try {
engine = requires.dust = require('dust');
} catch (err) {
try {
engine = requires.dust = require('dustjs-helpers');
} catch (err) {
engine = requires.dust = require('dustjs-linkedin');
}
}
}
var ext = 'dust'
, views = '.';
if (options) {
if (options.ext) ext = options.ext;
if (options.views) views = options.views;
if (options.settings && options.settings.views) views = options.settings.views;
}
if (!options || (options && !options.cache)) engine.cache = {};
engine.onLoad = function(path, callback){
if ('' == extname(path)) path += '.' + ext;
if ('/' !== path[0]) path = views + '/' + path;
read(path, options, callback);
};
try {
var tmpl = cache(options) || cache(options, engine.compileFn(str));
tmpl(options, fn);
} catch (err) {
fn(err);
}
};
So, how can I add custom helpers to dust in order for them to be available in templates?
Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.