2015-11-07 53 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