简体   繁体   English

Haml Coffee模板中的条件CSS类

[英]Conditional CSS Class In Haml Coffee Template

Give the following code in a .hamlc template: 在.hamlc模板中提供以下代码:

.myDiv
  some content here

What if I wanted to add the class "special" to the div.myDiv based on a property of the model passed to the template. 如果我想基于传递给模板的模型的属性将“特殊”类添加到div.myDiv中,该怎么办。

so if is_special property of the model was true, the output would be what is shown below: 因此,如果模型的is_special属性为true,则输出将如下所示:

<div class="myDiv special">
   some content here
</div>

and if is_special was false, the output would be: 如果is_special为false,则输出为:

<div class="myDiv">
  some content here
</div>

You can add a class attribute: 您可以添加一个类属性:

.myDiv{ class: 'hoho' }
  some content here

which will result in: 这将导致:

<div class='hoho myDiv'>
  some content here
</div>

Now you can add some CoffeeScript code to the attribute to make it dynamic: 现在,您可以向属性添加一些CoffeeScript代码以使其动态:

.myDiv{ class: @model.get('status') }
  some content here

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

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