简体   繁体   中英

Using mustache.js partials with express

So this may be a case of me completely misunderstanding functionality, but I am trying to use partials in node.js so that I have a reusable, reinsertable header and footer on my various templates similar to {% extends 'something.html' %} in django or <? includes 'something.php ?> <? includes 'something.php ?> in php. As I understand it this is what partials are for.

So in my app.js uses this configuration to render templates:

var mustache = require('mustache');
var template = {
    compile: function (source, options) {
        if (typeof source == 'string') {
            return function(options) {
                options.locals = options.locals || {};
                options.partials = options.partials || {};
                if (options.body) // for express.js > v1.0
                    locals.body = options.body;
                return mustache.to_html(
                    source, options.locals, options.partials);
            };
        } 
        else {
            return source;
        }
    },
    render: function (template, options) {
        template = this.compile(template, options);
        return template(options);
    }
};

// Configuration
app.configure(function(){
    app.register(".html", template);
    app.set('views', __dirname + '/views');
    app.set('view options', {layout: false});
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});

and then I have this route:

var header = require("../views/header.html");

module.exports = function(app){
app.all('/test', function(req, res){
    var data = {
        locals: {value: "some value"},
        partials: {header: header}
    }
    res.render('test.html', data);
});

header.html is simply this:

hello world

and test.html is simply this:

{{>header}}
{{ value }}

I would expect that this would render:

hello world
some value

but I get an unexpected token error when I run node app.js pointing to hello world in my header.html as the problem.

what am I missing in configuring this so that it will work?

For partials and how to make them work I would suggest taking a look at the consolidate.js project. It's an efford to integrate multiple template engines with express 3.x

This thread is very old but maybe someone beginner like me with mustache ends up like me here, with the same question.

In my case problem was I was omitting the ">" in the template..

  • express: "^4.17.1"
  • mustache-express: "^1.3.0"

in myRouter.ts

router.all('/', (req: Request, res: Response, next: NextFunction)  => {
  res.render('index', {
    pageTitle: 'Welcome',
    partial: res.render('partial',{...})
  });
});

in template.mustache

<html>
  <head>
    <title>{{pageTitle}}</title>
  </head>
  <body>
    {{>partial}}
   </body>
</html>

I wish this helps someone else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM