Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a server side mustache.js example using node.js

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); 
like image 691
onecoder4u Avatar asked Mar 22 '10 15:03

onecoder4u


People also ask

What is mustache in node js?

mustache. js is a zero-dependency implementation of the mustache template system in JavaScript. Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.

Is node js server side JavaScript?

Node. js is a server-side JavaScript run-time environment. It's open-source, including Google's V8 engine, libuv for cross-platform compatibility, and a core library.

How node js is server-side?

Node. js is a server-side, packaged software that contains predefined processes to accomplish specific tasks. As a server-side runtime, every Node. js process is executed on a server; essentially working on the backend aspect of an application to manage data.

Can Nodejs be client-side?

Being able to call Node. js modules from JavaScript running in the browser has many advantages because it allows you to use Node. js modules for client-side JavaScript applications without having to use a server with Node.


2 Answers

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);  
like image 84
AngusC Avatar answered Sep 21 '22 02:09

AngusC


Your example is almost correct. Mustache is an object, not a function, so it doesn't need the (). Rewritten as

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

will make it happier.

like image 21
Derek Gathright Avatar answered Sep 20 '22 02:09

Derek Gathright