Accessing data in handlebars file

How to access a variable in handlebars file that is declared in a JavaScript file. Accessing as below doesn’t work.

In JavaScript file(script.js):

(function() {
  var dataForCreate = "Create";
})();

In Handlebar file(sample.hbs):

<!doctype html>
<html>
  <head>
    <script src="script.js"></script>
  </head>
  <body>
    <button id="createBtn">{{dataForCreate}}</button>
  </body>
</html>

If you’re in JS, try something like this (assuming you’re traversing some of the sample app source code where express-hbs is used):

var hbs = require('express-hbs');

hbs.registerHelper('dataForCreate', function(foo) { return "Create"; });
3 Likes