I am a complete beginner with node.js.
What I try to do is to parse a jsrender template on server side
I donwloaded jsrender.js from git
this is my attempt ... saved as render.js:
var data = [
{id:1, name:"tom"},
{id:2, name:"jack"},
]
require('./jsrender.js', function(jsrender){
console.log('test');
var result = jsrender.render['<p>{{:id}} <b>{{:name}}</p>']( data );
console.log(result);
} );
and then runned it (node render.js)
and I get NOTHING
what am I doing wrong?
======================================================
tried this way too:
var data = [
{id:1, name:"tom"},
{id:2, name:"jack"},
]
var jsrender = require('./jsrender.js');
var result = jsrender.render('<p>{{:id}} <b>{{:name}}</p>',data );
console.log(result);
and I am getting
var result = jsrender.render('<p>{{:id}} <b>{{:name}}</p>',data );
^
TypeError: Object #<Object> has no method 'render'
========================================================================
tried also installing this node_jsrender module
and this syntax:
var jsrender = require('./jsrender');
process.on('start', function () {
jsrender.template("yourtemplate", "{{:myvar}}");
var result = jsrender.render("yourtemplate", {myvar:"Hello World!"});
console.log(result);
});
ALSO EMPTY result :(
So first you need to install the Node.js module.
npm install node_jsrender
This will create a node_modules directory with node_jsrender directory inside. Next you need to require.
var jsrender = require('node_jsrender');
If the first parameters of the require method starts with ./
it means that you want to import a local file. Without it Node.js will look at the node_modules directory.
jsrender.template("yourtemplate", "{{=myvar}}");
var result = jsrender.render("yourtemplate", {myvar:"Hello World!"});
I checked the syntax of that template engine and it's {{=
and not {{:
?