javascript 通过javascript访问把手变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30767928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:40:35  来源:igfitidea点击:

Accessing handlebars variable via javascript

javascriptnode.jsexpresshandlebars.js

提问by PokeRwOw

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.

我正在为 express js 框架生成一个把手视图,我需要访问我从单独的 JavaScript 文件中传递给视图的变量。

For example:

例如:

var foo = {{user.name}}

Someone got an idea? Helper?

有人有想法吗?帮手?

回答by Jonathan Lonowski

The value of user.nameneeds 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.

的值user.name需要尽可能有效的JavaScript表达式输出,如果你想将其包括在内的<script>,将被重新评估它的代码。

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

目前,如果user.name"john.doe",例如,结果脚本将是:

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


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

user.name至少需要加引号,所以它理解为一个字符串值/文本。

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.

您还可以利用 JSON 和 JavaScript 在语法上的相似性,将 JavaScript 可以理解的 JSON 输出为表达式,并且已经包含引号。

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:

请注意{{{...}}}最后一个示例中的三元组禁用 HTML 编码,因此您不会得到:

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

回答by Drkawashima

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

节点可以像这样将编码的 JSON 传递给车把视图:

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

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

把手视图可以像这样解码和解析 json:

<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.

由于传递给车把视图的任何内容基本上都会直接注入 HTML,因此您应该始终传递编码的 json 并让视图对其进行解码。



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.

我尝试了此线程中的其他建议。但是他们将未编码的 JSON 注入车把视图中,这总是给我解析错误。我不知道人们是如何让它工作的,因为 Handlebars 似乎会将变量解析为纯文本。

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.

我还没有读过 Handlebars 解析器是如何工作的——但在我看来,唯一安全的选择是使用编码的 JSON,就像我在我的例子中所建议的那样。