inline tag in haml

2020-05-17 00:18发布

In html, you can do something like this

<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</em>, ac venenatis ipsum. In sollicitudin, 
  lectus eget varius tincidunt, felis sapien porta eros, non 
  pellentesque dui quam vitae tellus. 
</p>

It is nice, because the paragraph of text still looks like a paragraph in the markup. In haml, it looks like this

%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. 

Is there any way to totally inline a tag in haml?

标签: haml
5条回答
一夜七次
2楼-- · 2020-05-17 00:24

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.

查看更多
疯言疯语
3楼-- · 2020-05-17 00:26

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.
查看更多
Summer. ? 凉城
4楼-- · 2020-05-17 00:31

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. 
查看更多
做自己的国王
5楼-- · 2020-05-17 00:38

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 :)

查看更多
ら.Afraid
6楼-- · 2020-05-17 00:45

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. 
查看更多
登录 后发表回答