a server side mustache.js example using node.js

onecoder4u picture onecoder4u · Mar 22, 2010 · Viewed 24.7k times · Source

I'm looking for an example using Mustachejs with Nodejs

here is my example but it is not working. Mustache is undefined. I'm using Mustachejs from the master branch.

var sys = require('sys');
var m = require("./mustache");

var view = {
  title: "Joe",
  calc: function() {
    return 2 + 4;
  }
};    
var template = "{{title}} spends {{calc}}";    
var html = Mustache().to_html(template, view);

sys.puts(html);

Answer

AngusC picture AngusC · Nov 12, 2011

I got your example working by installing mustache via npm, using the correct require syntax and (as Derek said) using mustache as an object not a function

npm install mustache

then

var sys = require('sys');
var mustache = require('mustache');

var view = {
  title: "Joe",
  calc: function() {
    return 2 + 4;
  }
};

var template = "{{title}} spends {{calc}}";

var html = mustache.to_html(template, view);

sys.puts(html);