Haml: Control whitespace around text

前端 未结 13 2153
时光说笑
时光说笑 2020-11-30 16:38

In my Rails template, I\'d like to accomplish final HTML to this effect using HAML:

I will first link somewhere         


        
13条回答
  •  星月不相逢
    2020-11-30 17:11

    Alright, here's the solution I'm settling on:

    Helper

    def one_line(&block)
      haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
    end
    

    View

    I will first
    - one_line do
      = link_to 'link somewhere', 'http://example.com'
      - if @condition
        , then render this half of the sentence
        \\n
        if a condition is met
    

    That way, whitespace is excluded by default, but I can still explicitly include it with a "\n" line. (It needs the double-backslash because otherwise HAML interprets it as an actual newline.) Let me know if there's a better option out there!

提交回复
热议问题