Available steering wheels variables through javascript

I am generating a handlebars view for a js framework expression, and I need to access the variables that I pass to the view from a separate JavaScript file.

For instance:

var foo = {{user.name}}

Anyone got an idea? Assistant?

Thanks for the advance!

PokeRwow

+4
source share
2 answers

The value user.nameshould be output as a valid JavaScript expression if you want to include it in <script>, which will re-evaluate it as code.

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

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

user.name at least it should be in quotation marks, so it is understood as a string value / literal.

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

JSON JavaScript , JSON, JavaScript , .

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

{{{...}}} , HTML, :

var foo = &quot;john.doe&quot;
+6

Node JSON handlebars :

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

- json :

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

, handlebars, HTML, json .

, unencoded JSON, . , handlebars . JSON.

+7

All Articles