Matt Briggs
Matt Briggs

Reputation: 42158

inline tag in haml

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?

Upvotes: 39

Views: 24590

Answers (5)

epicrato
epicrato

Reputation: 8408

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. 

Upvotes: 2

Quv
Quv

Reputation: 3139

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. 

Upvotes: 6

Sk&#228;ggiga Mannen
Sk&#228;ggiga Mannen

Reputation: 1043

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

Upvotes: 23

Rafa de Castro
Rafa de Castro

Reputation: 2524

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.

Upvotes: 10

Andrew Vit
Andrew Vit

Reputation: 19239

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.

Upvotes: 69

Related Questions