Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

jsRender and node.js

jsRender and node.js

Problem

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['

{{:id}} {{:name}}

']( 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('

{{:id}} {{:name}}

',data ); console.log(result);

and I am getting

var result = jsrender.render('

{{:id}} {{:name}}

',data ); ^ TypeError: 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 :(

Problem courtesy of: DS_web_developer

Solution

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 {{:?

Solution courtesy of: Pickels

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

jsRender and node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×