我们怎么能检查类似的东西
if( status == 0 ){ //show pending } if( status == 1) { // show ok } if( status == 2 ){ // done }这在Mustache图书馆。 我们可以在模板中提供逻辑吗?
how can we check something like
if( status == 0 ){ //show pending } if( status == 1) { // show ok } if( status == 2 ){ // done }this in Mustache Library. Can we provide logic in templates??
最满意答案
Mustache模板语言显然尽可能没有逻辑。
但是,您可以使用section构造来执行您想要的操作。 添加布尔值以挂起,确定并完成对象,然后执行:
{{#pending}} // show pending {{/pending}} {{#ok}} // show ok {{/ok}} {{#done}} // show done {{/done}}这实际上将比较逻辑移动到您的实际代码,这意味着模板可以保持逻辑更少。
(你在标签中提到了handlebars.js。如果你正在使用Handlebars,理论上你可以通过这样做来扩展语言,但这种做法违背了使用无逻辑模板语言的想法。你甚至可以抓住扩展的集合 ,但到那时我建议一起使用另一种模板语言。)
The Mustache template language is explicitly as logic-less as possible.
However, you can use the section construct to do what you want. Add booleans for pending, ok and done to your object, then do:
{{#pending}} // show pending {{/pending}} {{#ok}} // show ok {{/ok}} {{#done}} // show done {{/done}}This in effect moves the comparison logic to your actual code, which means the template can stay logic-less.
(You mentioned handlebars.js in your tags. If you're using Handlebars, you could theoretically extend the language by doing something like this, but that kind of goes against the idea of using a logic-less template language. You could even grab a collection of extensions, but by then I'd recommend going for another template language altogether.)
更多推荐
发布评论