本文介绍了有什么方法可以避免在不创建新文件的情况下进行haml代码重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Sinatra应用程序的.haml模板文件中包含以下代码:
I have the following code in a .haml template file in a Sinatra app:
- if(@order == 'inverse')
- @list.reverse_each do |item|
.item
%span.action-move(data-icon="o")
.detail.title=item[0]
.detail.content=item[1]
%span.action-delete(data-icon="d")
- else
- @list.each do |item|
.item
%span.action-move(data-icon="o")
.detail.title=item[0]
.detail.content=item[1]
%span.action-delete(data-icon="d")
如您所见,5行代码是相同的.有没有一种方法可以重构此代码,以免在这里不重复创建一个额外的文件以用作部分文件?
As you can see, 5 lines of code are identical. Is there a way that I can refactor this code to avoid the duplication here without creating an additional file to use as a partial?
推荐答案
离我远去-您可以创建一个临时列表,该模板将在条件语句中设置,然后像这样循环遍历该临时列表:
Off the top of my head - you could create a temp list that you would set in the conditionals and then loop through the temp list like so:
- if(@order == 'inverse')
- temp = @list.reverse
- else
- temp = @list
- @temp.each do |item|
.item
%span.action-move(data-icon="o")
.detail.title=item[0]
.detail.content=item[1]
%span.action-delete(data-icon="d")
这篇关于有什么方法可以避免在不创建新文件的情况下进行haml代码重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!