有条件地测试是否在HAML模板中添加类的巧妙方法 [英] Neat way to conditionally test whether to add a class in HAML template

查看:86
本文介绍了有条件地测试是否在HAML模板中添加类的巧妙方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如果Haml中的条件为true,则附加类(使用Rails)

Possible Duplicate:
Append class if condition is true in Haml (with Rails)

我正在使用一个模板,该模板允许您将列表项标记为current(使用class=current),并在导航栏中将其突出显示.

I'm using a template that allows you to mark a list item as current (using class=current), highlighting it in a nav bar.

在HAML中,这看起来像:

In HAML, this looks like:

%li.current
  Menu item A
%li
  Menu item B
%li
  Menu item C

我在Sinatra视图中具有此代码,并希望通过编程方式添加class=current,具体取决于视图的参数.

I have this code in a Sinatra view and want to programmatically add the class=current, depending on a parameter to the view.

如何以最简洁的方式做到这一点?

目前,我正在这样做:

  - if section == "pages"
    %li.current
      %a{:href => "#pages"} Pages
  - else
    %li
      %a{:href => "#pages"} Pages

感觉太冗长了.

推荐答案

您可以内联条件以确定%li是否需要该类

You can inline the conditional to determine if the class is needed for the %li

%li{ :class => ('current' if section == "pages") }
  %a{ :href => "#pages" } Pages

这篇关于有条件地测试是否在HAML模板中添加类的巧妙方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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