Reputation: 1292
I have a JSON data in dataList variable and I want to add this in custom handlebars helper as a parameter.
var dataList = [
{
"id": 1,
"title": "Arrange meeting",
"date": "Today 10:35 | By Admin",
"completed": true
},
];
Handlebars Custom helper
var Handlebar = require('handlebars');
Handlebar.registerHelper('List', function(data, options){
//console.log(data) //returns undefined
return options.fn(JSON.parse(data)); // gives error
});
Custom Helper Rendering:
{{#List dataList}}
{{> widgets/toDoList}}
{{/List}}
It produce error
SyntaxError: Unexpected token u in JSON at position 0
Whereas, it is working with the static data
{{#List '[
{
"id": 1,
"title": "Arrange meeting",
"date": "Today 10:35 | By Admin",
"completed": true
},
]' }}
{{> widgets/toDoList}}
{{/List}}
Upvotes: 3
Views: 1470
Reputation: 3707
I am pretty much sure, it's not the problem of custom helper, but the way you are passing data to it.
If you are using express, it would be like res.render('view-name', data)
Upvotes: 1
Reputation: 700
it's becuase your data
return undefined
see for more here [ uncaught syntaxerror unexpected token U JSON ]
Upvotes: 0