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

8

我对Ruby on Rails非常陌生,对Haml更是一无所知,两个小时前我才开始使用它。我正在跟随一个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.'

我也尝试过这样做:

: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.'
1个回答

14
你可以使用 #{} 在文本块中包装 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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接