gpt4 book ai didi

ruby-on-rails - haml 条件 if/else 缩进

转载 作者:行者123 更新时间:2023-12-03 16:04:52 30 4
gpt4 key购买 nike

我有一种情况,只有当计数大于 0 时,我才想向 div 标签添加一个类

例子:

- @color.shades.each_with_index do |shade, index|
- if index == 0
#shades
- else
#shades.horizontalLine.second
%h3 something
%dl
%dt some
%dd some1

在这个例子中,我想要从 %h3 开始的所有内容属于 #shades#shades.horizontalLine.second然而,取决于那些 if 和 else 语句的评估。

解决方法是:
- @color.shades.each_with_index do |shade, index|
- if index == 0
#shades
%h3 something
%dl
%dt some
%dd some1
- else
#shades.horizontalLine.second
%h3 something
%dl
%dt some
%dd some1

但在这里我必须重复代码

我很难过如何在 Rails 中执行此操作而不重复从 %h3 开始的代码对于两个 div。

最佳答案

您可以根据索引将类设置为保存类名称的变量以将其干燥:

- @color.shades.each_with_index do |shade, index|
- shade_classes = index == 0 ? '' : 'horizontalLine second'
#shades{ :class => shade_classes }
%h3 something
%dl
%dt some
%dd some1

关于ruby-on-rails - haml 条件 if/else 缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7265146/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com