Haml formatting

I'm new to haml, so I'm still trying to figure out the formatting.

I have an index.haml file with the following code.

%h1 Welcome to Solidarity Hello, = @profile.first_name ! 

It looks like this:

Welcome to solidarity
Hello, user!

Here's the page source:

 <h1> Welcome to Solidarity </h1> Hello, frances ! 

It has a space between @ profile.first_name and the exclamation mark. Why is this? And how can I fix this?

+4
source share
2 answers
 %h1 Welcome to Solidarity Hello, #{@profile.first_name}! Please #{link_to 'post a comment', new_comment_path}! 

becomes

 <h1>Welcome to Solidarity</h1> Hello, John! Please <a href="/comments/new">post a comment</a>! 

Please keep in mind that in Rails 2 and Haml 2 you must correctly html-run everything that you send to the browser (ht nex3):

 Hello, #{h @profile.first_name}! 

In Rails 3 and Haml 3, everything is disabled by default, so you can simply do:

 Hello, #{@profile.first_name}! 
+5
source

You can also use alligators to eat up the space before or after the tag. From haml-lang docs :

 %img %pre>< foo bar %img 

compiled for:

 <img /><pre>foo bar</pre><img /> 

Although it would also solve your problem here, the solution given by Justice is the appropriate markup for this scenario. Just thought that I mention this.

+5
source

Source: https://habr.com/ru/post/1307979/


All Articles