在MustacheJs中切换外壳 [英] Switch case in MustacheJs

查看:86
本文介绍了在MustacheJs中切换外壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Mustache js(逻辑性较差的模板)是否可以实现切换情况?这是我需要的,因为基于值将一个类分配给dom元素,例如:

Using Mustache js (a logic less templating) is there a way i can achieve switch case? this i need because a class is assigned to dom element based on the value for example:

switch(tasks.Count)
{
   case 0:
       element.Class = "no-tasks";
    break;
   case 1:
       element.Class = "one-tasks";
   break;
.
.
.
}

这就是我现在得到的代码,我该如何将其转换为模板(我相信呈现模型的方法是一个选择),但是添加方法来确定要使用哪个类是一种矫kill过正的方法,而且这还会污染环境我的模特陷入沼泽!

that's the code i got now, how do i transform it to template( I Believe having methods on model being rendered is the one option) But adding methods to determine which class to use is an overkill and besides that is going to pollute my model to a swamp!!

  • 我之所以这样问,是因为我在Nustache上将MustacheJs移植到C#、. NET来渲染嵌套模型.适用于Mustache的任何内容也适用于Nustache

推荐答案

有几种方法可以做到这一点.

There's a few ways to do this.

在Javascript中,如果Mustache在值中遇到函数,它将使用附带的文本作为唯一参数来调用它.

In Javascript, if Mustache encounters a function in a value, it will call it using the enclosed text as the only argument.

var data = {
    foo: function(text) { return '<b>' + text + '</b>'; }
}

胡子

{{#foo}}
   HI I LIKE FISH, thanks.
{{/foo}}

输出

<b>HI I LIKE FISH, thanks.</b>

在小胡子文档中搜索"lambda".

Search for "lambda" in the mustache docs.

另一种方法是进行虚假/真实检查.

Another way to do it is do a falsey/truthy check.

数据

{ foo: true }

胡子

{{#foo}}
  output this if true.
{{/foo}}
{{^foo}}
  output if false
{{/foo}}

这篇关于在MustacheJs中切换外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆