Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing handlebars variable via javascript

I'm generating a handlebars view for express js framework, and I need to access the variables I pass to the view from inside a separate JavaScript file.

For example:

var foo = {{user.name}}

Someone got an idea? Helper?

like image 253
PokeRwOw Avatar asked Jun 10 '15 21:06

PokeRwOw


People also ask

Can you use handlebars in JavaScript?

Handlebars. js is a Javascript library used to create reusable webpage templates. The templates are combination of HTML, text, and expressions. The expressions are included in the html document and surrounded by double curly braces.

How do I register helper handlebars?

js or <script> tag or anywhere else, you need to have access to Handlebars object, since you are invoking registerHelper method on that object (which injects a helper into that object). After Handlebars has been modified, every module referencing that object will have access to the new helper.


2 Answers

The value of user.name needs to be output as a valid JavaScript expression if you want to include it in within a <script>, which will be reevaluating it as code.

Currently, if user.name is "john.doe" for example, the resulting script will be:

var foo = john.doe; // `john` is treated as an object with a `doe` property

The user.name at least needs to be in quotes so it's understood as a string value/literal.

var foo = "{{user.name}}";
// result
var foo = "john.doe";

You can also take advantage of JSON and JavaScript's similarities in syntax, outputting JSON that JavaScript can understand as an Expression, and will already include the quotes.

Handlebars.registerHelper('json', function (content) {
    return JSON.stringify(content);
});
var foo = {{{json user.name}}};
// result
var foo = "john.doe";

Note the triple {{{...}}} in the last example to disable HTML encoding, so you don't end up with:

var foo = &quot;john.doe&quot;
like image 107
Jonathan Lonowski Avatar answered Oct 20 '22 00:10

Jonathan Lonowski


Node can pass encoded JSON to the handlebars-view like this:

result.render('index', { 
  encodedJson : encodeURIComponent(JSON.stringify(jsonData))
});

The handlebars-view can decode and parse the json like so:

<script>
  var decodedJson = decodeURIComponent("{{{encodedJson}}}");
  var jsonObj = JSON.parse(decodedJson);
</script>

Since anything passed to a handlebars-view will basically be injected straight into HTML, you should always pass encoded json and let the view decode it.


I tried the other suggestions in this thread. But they injected unencoded JSON into the handlebars view and that always gave me parsing errors. I have no idea how people got that to work, since it seems like Handlebars will parse variables as plain text.

I have not read up on how the Handlebars parser works - but it seems to me the only safe option is to use encoded JSON, like how I suggest in my examples.

like image 29
Drkawashima Avatar answered Oct 20 '22 00:10

Drkawashima