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);
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.
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With