开发者

Haml formatting

开发者 https://www.devze.com 2022-12-28 16:46 出处:网络
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.

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.fi开发者_C百科rst_name
!

It renders 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 that? And, how do I fix it?


%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 properly html-escape anything you send to the browser (ht nex3):

Hello, #{h @profile.first_name}!

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

Hello, #{@profile.first_name}!


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

%img
%pre><
  foo
  bar
%img

is compiled to:

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

While this would have also solved your problem here, the solution given by Justice is the appropriate markup for this scenario. Just thought I'd mention it.

0

精彩评论

暂无评论...
验证码 换一张
取 消