Partial rendering in Express.js and mustache

I am trying to partially download Mustache in express.js, but obviously I am having problems. Is it possible to use this EJS approach when a partial file of the form is included:

Node.js - EJS - including partial

I have a template file called index.html

<html>
    <head>
        <title>My Title</title>
    </head>
    <body>
        {{>header}}
    </body>
</html>

and header.mustache

<div class="header">
    <div style="display: table-cell;padding-left: 10px;padding-top: 8px;vertical-align: middle;">
        <img src="images/logo.png">
    </div>
</div>

In express.js, my rendering function looks something like this:

app.get('/', function(req, res) {
    res.render('signup.html', {
        partials: {
            header: partial('header.mustache')   //this line is wrong        
        }
    });
});

I am new to this, but would like to somehow display or read the entire contents of the header.mustache for the header object. Is it possible?

+3
source share
1 answer

Take a look at the following link, which can explain how you can customize your mustache with express.js

Using Mustache Templates in Express

, , tmpl.compile

compile : function(source, options) {
    var views = options.views || './views';
    var extension = options.extension || '.html';

    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;

            for(var p in options.partials) {
                var partialFileName = views + '/' + options.partials[p] + extension;

                if(path.existsSync(partialFileName)) {
                    options.partials[p] = fs.readFileSync(partialFileName, "utf-8");
                }
            }               
            return mustache.to_html(source, options.locals, options.partials);
        };
    } else {
        return source;
    }
}

2

var fs = require("fs"),
var path = require("path");

, views .html ( .html )

app.get('/', function(req, res) {
    res.render('signup.html', {
        partials: {
            header: 'header'   //this now works :)
        }
    });
});
+1