是否可以在HAML的link_to helper裏添加html-content?把haml標籤放在link_to helper裏
我想這一點,但我得到的是一個語法錯誤:
= link_to "Other page", "path/to/page.html"
%span.icon Arrow
預期輸出:
<a href="path/to/page.html">Other Page<span class="icon">Arrow</span></a>
是否可以在HAML的link_to helper裏添加html-content?把haml標籤放在link_to helper裏
我想這一點,但我得到的是一個語法錯誤:
= link_to "Other page", "path/to/page.html"
%span.icon Arrow
預期輸出:
<a href="path/to/page.html">Other Page<span class="icon">Arrow</span></a>
如果有人仍在使用,您應該使用塊
= link_to "path/to/page.html" do
Other page
%span.icon Arrow
Rails 2.x在一個項目中,它看起來像接受的答案返回塊,從而複製標記中的鏈接。很簡單的變化:使用-
代替=
- link_to "path/to/page.html" do
Other page
%span.icon Arrow
最簡單的方法來做到這一點是通過使用html_safe或原始功能
= link_to 'Other Page<span class="icon"></span>'.html_safe, "path/to/page.html"
或使用原始功能(推薦)
= link_to raw('Other Page<span class="icon"></span>'), "path/to/page.html"
簡單,因爲它可以得到!
除非確定字符串不爲零,否則不要使用html_safe方法。而是使用raw()方法,該方法在nil上不會引發異常。
你知道我怎樣才能擺脫鏈接結尾處由該代碼創建的額外空間嗎?當整個鏈接變成下劃線時,我將鼠標懸停在它上面會很明顯。 – ayjay 2014-10-20 22:09:27
謝謝你,這是非常有用的。您錯過了解釋,在'link_to'中使用'do'塊時,唯一的參數是路徑,並且鏈接的文本或其他任何內容都在塊中。 – unmultimedio 2016-12-07 16:34:06
如果您使用[font-awesome-sass gem](https://github.com/FortAwesome/font-awesome-sass#a-ruby-on-rails),您還可以使用例如'=圖標「箭頭 - 右側」'而不是調用輔助方法的'%span.icon Arrow'。 – metakermit 2017-10-17 21:21:59