inline tag in haml

前端 未结 5 1965
余生分开走
余生分开走 2021-01-31 15:31

In html, you can do something like this

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eget aliquet odio. Fusce id quam eu augu

相关标签:
5条回答
  • 2021-01-31 16:15

    It's all about indentation:

    %p
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eget aliquet odio. Fusce id quam eu augue sollicitudin imperdiet eu ac eros. 
    
      %em 
        Etiam nec nisi lorem, ac venenatis ipsum. In sollicitudin, lectus eget varius tincidunt, felis sapien porta eros, non pellentesque dui quam vitae tellus. 
    
    0 讨论(0)
  • 2021-01-31 16:17

    You can inline HTML in any HAML doing

    %p!= "Lorem ipsum <em>dolor</em> sit amet"
    

    The != operator means that whatever the right side returns it will be outputted.

    0 讨论(0)
  • 2021-01-31 16:19

    As a hybrid of these nice answers by others, I think you can define a Helper method in your application_helper.rb for some inline markups you'd frequently use. You don't need to mix HTML with HAML, nor do you have to type much.

    In your helper;

    def em(text)
      content_tag(:em, text)
    end
    
    #def em(text)
    #  "<em>#{text}</em>".html_safe
    #end
    

    In your haml;

    %p
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eget 
        aliquet odio. Fusce id quam eu augue sollicitudin imperdiet eu ac eros. 
        #{em 'Etiam nec nisi lorem'}, ac venenatis ipsum. In sollicitudin, 
        lectus eget varius tincidunt, felis sapien porta eros, non 
        pellentesque dui quam vitae tellus. 
    
    0 讨论(0)
  • 2021-01-31 16:23

    Haml excels for structural markup, but it's not really intended for inline markup. Read: Haml Sucks for Content. Just put your inline tags as HTML:

    .content
      %p
        Lorem ipsum <em>dolor</em> sit amet.
    

    Or else use a filter:

    .content
      :markdown
        Lorem ipsum *dolor* sit amet.
    
    0 讨论(0)
  • 2021-01-31 16:28

    I know this is old. But figured I'd post this in case anyone lands here. You can also do this sort of thing in haml (And maybe more what the OP was looking for?).

    %p Here is some text I want to #{content_tag(:em, "emphasize!")}, and here the word #{content_tag(:strong, "BOLD")} is in bold. and #{link_to("click here", "url")} for a link.
    

    Useful for those situations where doing it on multiple lines adds spaces you don't want I.E. When you have a link at the end of a sentence, and don't want that stupid space between the link and the period. (or like in the OP's example, there would be a space between the and the comma.

    Just don't get carried away like i did in the example :)

    0 讨论(0)
提交回复
热议问题