【问题标题】:Is this the correct way to display/concatenate text using Haml?这是使用 Haml 显示/连接文本的正确方法吗?
【发布时间】:2011-06-19 04:30:10
【问题描述】:

我对 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.'

我也试过这个:

: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-on-rails-3 haml


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2011-10-07
      • 2011-09-26
      • 1970-01-01
      • 2012-05-11
      • 2018-12-31
      • 2021-04-29
      相关资源
      最近更新 更多