12

I want to be able to have two Haml elements on the same line. For example:

%h1 %a{:href => '/'} Professio.

That doesn't work. How would I get something like this to work without borking?

4 Answers 4

19

Late answer, but I was just tackling the same problem myself and knew HAML had a way to do this as part of the language. See the White Space Removal section of the HAML reference.

You could do it this way:

%h1<
  %a{ :href => '/' } Professio.

Or this way:

%h1
  %a{ :href => '/' }> Professio.
7

Unfortunately, you cannot put two haml elements on the same line.

You can achieve something similar by using inline html elements:

%h1 <a href='/'>Lorem ipsum</a>

Why don't you like this format?

%h1 
  %a{:href => '/'} Professio.

Another option is to write special 'helper' method (that generate an html link). For example, the link_to in Rails:

%h1= link_to 'Professio', root_url
2
  • I would like it on the same line because I want the output HTML to be on the same line. I'll just do multiple lines, thanks. Mar 13, 2010 at 14:34
  • 1
    Check Garvin answer bellow, he got late but seems to be the right one for this question.
    – fotanus
    Jun 20, 2012 at 21:50
5

Haml cannot do this. Slim can:

h1: a( href='/' ) Professio.

is the same as:

h1
  a( href="/ ) Professio

You can write as deeper tree as you need:

ul.nav
  li.active: a( href="/home" ):    strong Home
  li:        a( href="/contact" ): span   Contact

Jade also has similar syntax and support this feature, but it's been designed for Node.js environment.

1
  • Thanks for pointing out Slim, it seems like a nice alternative to HAML! Jun 29, 2015 at 15:00
3

If you're looking to preserve the HTML on the same line you could try something like this:

irb> print Haml::Engine.new("%h1<\n  %a{:href => '/'} Profession.").render()
<h1><b href='/'>Profession.</a></h1>

Found here: HAML whitespace removal

[Edit: I know that's says b href above...]

1
  • I posted my answer after I realized your answer was essentially the same. I didn't realize it was the same at first because the code sample is kind of obfuscated.
    – Jim Garvin
    Apr 20, 2011 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.