gpt4 book ai didi

ruby-on-rails - 为什么 HAML(在 Ruby of Rails 中)不能选择性地输出一个 而只输出一个 ?

转载 作者:行者123 更新时间:2023-12-04 06:11:59 26 4
gpt4 key购买 nike

下面的代码是输出一个2列表——输出<tr>仅每 2 项;否则,只需一个 <td> ,但我发现输出有 <tr>一直以来,对于每一个 <td> ,这是为什么呢——是bug吗?

- arr = [2, 4, 6, 8, 10, 12]
%table
- arr.each_with_index do |num, i|
- if i % 2 == 0
%tr
%td= "column 1: #{num}"
- else
%td= "column 2: #{num}"

最佳答案

您也可以使用 each_slice 代替 each_with_index。 each_slice(2) 将原始数组分成 3 组,每组 2 个数组,因此更容易使用 haml 进行迭代。也少了一点代码。

- arr = [2, 4, 6, 8, 10, 12]

%table
- arr.each_slice(2).each do |row|
%tr
%td= "column 1: #{row[0]}"
%td= "column 2: #{row[1]}"

关于ruby-on-rails - 为什么 HAML(在 Ruby of Rails 中)不能选择性地输出一个 <tr> 而只输出一个 <td>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5675199/

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