在Mustache模板中实现if条件(Implementing if conditions in Mustache template)

编程入门 行业动态 更新时间:2024-10-14 00:27:45
在Mustache模板中实现if条件(Implementing if conditions in Mustache template)

我们怎么能检查类似的东西

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

更多推荐

本文发布于:2023-07-30 18:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   条件   Mustache   template   conditions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!