这是使用Haml显示/连接文本的正确方法吗?

Jor*_*rte 7 haml ruby-on-rails-3

我对Ruby on Rails很新,甚至比Haml更新,我2小时前开始使用它.所以我遵循Ruby on Rails教程并决定在视图上使用Haml,但我不确定这是否是显示事物的正确方法(感觉有点奇怪).有人可以开导我吗?:)

%h1= "About Us"
%p
  =link_to 'Ruby on Rails Tutorial','http://railstutorial.org' 
  &= 'is a project to make a book and screencasts to teach web development with'
  &= link_to 'Ruby on Rails', 'http://rubyonrails.org'
  &= '. This is the sample application for the tutorial.'
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

:ruby
   first_link = link_to 'Ruby on Rails Tutorial','http://railstutorial.org' 
   second_link = link_to 'Ruby on Rails', 'http://rubyonrails.org'

%h1= "About Us"
%p
  = first_link
  &= 'is a project to make a book and screencasts to teach web development with'
  &= second_link
  &= '. This is the sample application for the tutorial.'
Run Code Online (Sandbox Code Playgroud)

mat*_*att 11

您可以使用#{}在一个文本块中包装ruby代码,如果您只想要文本(例如在您的文本中%h1),则不需要使用=.

%h1 About Us
%p
  #{link_to 'Ruby on Rails Tutorial','http://railstutorial.org'} is a project to make a book and screencasts to teach web development with #{link_to 'Ruby on Rails', 'http://rubyonrails.org'}. This is the sample application for the tutorial.
Run Code Online (Sandbox Code Playgroud)

如果你想打破这一行,以便在编辑器中更容易处理,请确保每行都缩进相同的数量,并且不要在Ruby函数的中间拆分:

%h1 About Us
%p
  #{link_to 'Ruby on Rails Tutorial','http://railstutorial.org'} is a project to make a
  book and screencasts to teach web development with #{link_to 'Ruby on Rails', 'http://rubyonrails.org'}.
  This is the sample application for the tutorial.
Run Code Online (Sandbox Code Playgroud)