2015-11-07 52 views
2

我必須渲染一個或一個條件,但是,苗條的縮進和「沒有明確的結束允許」的方式。縮進哎呀,如果「如果條件<a>其他<span>」slim

在這個簡化的代碼中,我想根據產品是否可用,將包含三個DIV的產品包裝在A或SPAN中。


- if available? 
    a href=... 
- else 
    span class="unavailable" 
.currency = @product.currency 
.price = @product.price 
.description = @product.description 

以上顯然不會工作,因爲產品沒有A或跨度內的渲染,但在它旁邊。


- if available? 
    a href=... 
- else 
    span class="unavailable" 
    .currency = @product.currency 
    .price = @product.price 
    .description = @product.description 

這種情況甚至更糟,因爲苗條對待產品爲「其他」一案的一部分,使旁邊的SPAN,而不是產品都爲A.


- if available? 
    a href=... 
- else 
    span class="unavailable" 
    .currency = @product.currency 
    .price = @product.price 
    .description = @product.description 

這裏一切都很好的SPAN,但A仍然是空的,因爲該產品被視爲「其他」案件的一部分。現在


- if available? 
    a href=... 
- else 
    span class="unavailable" 
- end 
    .currency = @product.currency 
    .price = @product.price 
    .description = @product.description 

,這可能是正確的,但苗條不允許明確的「結束」,因此會引發異常。

任何想法如何讓這個工作? PS:也許苗條應該允許明確的「結束」像這樣的邊緣情況。

+0

我gues您可以通過這種方式創建一個部分來保存產品,然後從條件的兩邊呈現相同的結果。所以,' - 如果有的話?一個href = ...(渲染產品) - 否則span class =「unavailable」(渲染產品)'希望你明白了,因爲我似乎無法得到很好的評論格式。 :) –

回答

0

這是我能想出的解決辦法:

使用capturing to local variables保存您的孩子在一個局部變量:

= capture_to_local children=:children 
    .currency = @product.currency 
    .price = @product.price 
    .description = @product.description 

現在你可以運行

- if available? 
    a href=... 
    = children 
- else 
    span class="unavailable" 
    = children 

我沒有苗條的設置,只是試着用哈姆,但它應該工作只是相同:)

+0

它不會贏得美容獎品,但它是一個足夠好的解決方法。但是,因爲它是一個Rails應用程序,所以我使用'capture'代替。感謝您的靈感! – svoop