node.jsexpresstemplate-enginemarko

How to set marko template engine in expressjs 4.0


I want to use marko template engine along with expressjs 4.0 When I remove the default express template engine I am getting following error

Error: No default engine was specified and no extension was provided. at new View (E:\nodeleap\node_modules\express\lib\view.js:48:42) at EventEmitter.app.render (E:\nodeleap\node_modules\express\lib\application.js:545:12) at ServerResponse.res.render (E:\nodeleap\node_modules\express\lib\response.js:938:7) at E:\nodeleap\config\express.js:106:19 at Layer.handle [as handle_request] (E:\nodeleap\node_modules\express\lib\router\layer.js:82:5) at trim_prefix (E:\nodeleap\node_modules\express\lib\router\index.js:302:13) at E:\nodeleap\node_modules\express\lib\router\index.js:270:7 at Function.proto.process_params (E:\nodeleap\node_modules\express\lib\router\index.js:321:12) at next (E:\nodeleap\node_modules\express\lib\router\index.js:261:10) at Layer.handle [as handle_request] (E:\nodeleap\node_modules\express\lib\router\layer.js:78:12)


Solution

  • It looks like marko is not yet supported by express view engines (see here: https://github.com/tj/consolidate.js)

    Edit: But the Marko FAQ recommends not using the express view engine. Option 1 below should work.

    So I see a few options, 1. Simply render your templates using marko directly in each of your routes or 2. Set up your own express view engine, and maybe even 3. Make a PR to consolidate.js to add marko support so that others can enjoy :)

    Options 1 and 2 would look something like this:

    var express = require('express');
    var app = express();
    var fs = require('fs');
    var marko = require('marko');
    
    // Option 2
    app.engine('marko', function(filePath, options, callback) {
        marko.load(filePath).render(options, function(err, output) {
            callback(null, output);
        });
    });
    
    app.set('views', './views');
    app.set('view engine', 'marko');
    
    app.get('/viewengine', function(req, res) {
        res.render('hello', {
            name: 'Frank'
        });
    });
    
    // Option 1
    var template = marko.load(require.resolve('./views/hello.marko'));
    app.get('/marko', function(req, res) {
        template.stream({
            name: 'Frank'
        })
            .pipe(res);
    });
    
    var server = app.listen(3000, function() {
        var host = server.address().address;
        var port = server.address().port;
        console.log('Example app listening at http://%s:%s', host, port);
    });