ruby on rails 3-这是使用Haml显示/连接文本的正确方式吗



我对RubyonRails非常陌生,对Haml来说更新,我2小时前就开始使用它了。因此,我遵循RubyonRails教程,并决定在视图中使用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.'

我也试过这个:

: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.'

您可以使用#{}将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.

如果你想打断这行以便在编辑器中更容易处理,请确保每行的缩进量相同,并且不要在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.

最新更新