在 Handlebars 模板中执行 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13654009/
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-26 19:22:52  来源:igfitidea点击:

Executing javascript inside Handlebars template

javascripttemplate-enginehandlebars.js

提问by jpeltoniemi

I'm totally new to js template engines. Handlebars seems to be the popular choice. I don't dislike the syntax for doing conditions, loops and so on, but since I'm perfectly capable of and feel more comfortable using plain old js and I'm not planning to let anyone who doesn't know js touch my templates, I'm asking if Handlebars supports this.

我对 js 模板引擎完全陌生。把手似乎是流行的选择。我不喜欢用于执行条件、循环等的语法,但是因为我完全有能力并且感觉使用普通的旧 js 更舒服,而且我不打算让任何不了解 js 的人接触我的模板,我在问 Handlebars 是否支持这个。

Of course the most popular choice isn't always the best. I'm more of a Mootools guy and jQuery drives me crazy(great library, just not for me). So if Handlebars was jQuery of template engines, what would be Mootools?

当然,最受欢迎的选择并不总是最好的。我更像是一个 Mootools 人,jQuery 让我发疯(很棒的库,只是不适合我)。那么如果 Handlebars 是模板引擎的 jQuery,那么 Mootools 会是什么?

采纳答案by Peter Lyons

One of the central ideas behind handlebars and mustacheis they are LOGICLESSby design and intention. They can not now, nor will theyever allow or recommend you embed raw JS in your templates. Some say this is a better design. If you want embedded javascript, almost every other traditional templating engine, such as _.templateor jadeor EJSis based on (or at least supports) the idea of embedded JS code snippets.

车把和胡须背后的中心思想之一是它们的设计和意图是无逻辑的。他们现在不能,也不会他们以往任何时候都允许或推荐你在你的模板嵌入生JS。有人说这是一个更好的设计。如果您想要嵌入式 javascript,几乎所有其他传统模板引擎,例如_.templatejadeEJS,都基于(或至少支持)嵌入式 JS 代码片段的想法。

回答by Doctor Parameter

Sorry to resurrect a old issue here. If you have to use handlebars or it's not feasible to change your templating library you can use Helpers from the Handlebars library.

很抱歉在这里复活了一个旧问题。如果您必须使用把手或者更改模板库不可行,您可以使用把手库中的帮助程序。

http://handlebarsjs.com/expressions.html#helpers

http://handlebarsjs.com/expressions.html#helpers

回答by Ogbonna Vitalis

Registering a helper in handlebar can be a walk around to what you are trying to do. See this link for a complete example: Registering handlebars helpers with node does absolutely nothing

在车把中注册一个助手可以让你四处走走看看你正在尝试做的事情。有关完整示例,请参阅此链接:Registering handlebars helpers with node 绝对没有任何作用