put haml tags inside link_to helper

拥有回忆 提交于 2019-11-27 11:05:02

问题


is it possible to add html-content inside a link_to helper in HAML?

i tried this, but all i get is a syntax error:

= link_to "Other page", "path/to/page.html"
    %span.icon Arrow

expected output:

<a href="path/to/page.html">Other Page<span class="icon">Arrow</span></a>

回答1:


You should use block

= link_to "path/to/page.html" do
  Other page
  %span.icon Arrow



回答2:


If anyone is still using Rails 2.x on a project, it looks like the accepted answer returns the block, thus duplicating the link in the markup. Very simple change: use - instead of =

- link_to "path/to/page.html" do
  Other page
  %span.icon Arrow



回答3:


The simplest way to do it is by using html_safe or raw functions

= link_to 'Other Page<span class="icon"></span>'.html_safe, "path/to/page.html"

or using raw function (recommended)

= link_to raw('Other Page<span class="icon"></span>'), "path/to/page.html"

Simple as it can get !!

Don’t use html_safe method unless you’re sure your string isn’t nil. Instead use the raw() method, which wont raise an exception on nil.



来源:https://stackoverflow.com/questions/9618971/put-haml-tags-inside-link-to-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!