简体   繁体   English

在JavaScript中使用把手模板变量

[英]Using handlebars template variable in javascript

I would like to use a template variable Handlebars in javascript. 我想在javascript中使用模板变量Handlebars。 Something like this: 像这样:

<script>
var a = {{name}};
</script>

Thanks 谢谢

Taking the cue from your explanatory comment, I'd say that you're thinking about this the wrong way. 从您的解释性评论中得出的线索,我想您是在以错误的方式思考这一问题。 You want to do this: 您想这样做:

var template = Handlebars.compile(source);
$("#content").html(template({ name: "test" }));

var a = {{name}};

But what you should really do is: 但是,您真正应该做的是:

var template = Handlebars.compile(source);
var vals = { name: "test" };
$("#content").html(template(vals));

var a = vals.name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM