肯定的:
figure_helper(:foo, 'path/to/img.png', 'caption text here')
然後在您的幫助文件:
def figure_helper(cls='default_cls', img='rails.png', caption='uh oh! forgot caption!')
<<STMT
<figure class="#{cls}">
<img src="#{img}" />
<figcaption>#{caption}</figcaption>
</figure>
STMT
end
注意:如果你不熟悉定義字符串這裏語句的語法,確保關閉<<STMT
開始在第一列。
在IRB:
1.9.2p0 :016 > def figure_helper(cls='default_cls', img='rails.png', caption='uh oh! forgot caption!')
1.9.2p0 :017?> <<STMT
1.9.2p0 :018"> <figure class="#{cls}">
1.9.2p0 :019"> <img src="#{img}" />
1.9.2p0 :020"> <figcaption>#{caption}</figcaption>
1.9.2p0 :021"> </figure>
1.9.2p0 :022"> STMT
1.9.2p0 :023?> end
=> nil
1.9.2p0 :025 > puts figure_helper(:foo, 'path/to/img.png', 'caption text here')
<figure class="foo">
<img src="path/to/img.png" />
<figcaption>caption text here</figcaption>
</figure>
=> nil
謝謝,這是很有幫助的。你介意給我看幫手代碼的樣子嗎?如果某些變量是可選的,助手會如何處理? –
發表了一個工作解決方案。 – Anil