2011-06-19 19 views
7

Tôi rất mới với Ruby on Rails, và thậm chí mới hơn với Haml, tôi bắt đầu sử dụng nó 2 giờ trước. Vì vậy, tôi đang theo một hướng dẫn Ruby on Rails và quyết định sử dụng Haml trên quan điểm, nhưng tôi không chắc chắn nếu đây là cách chính xác để hiển thị mọi thứ (nó cảm thấy kinda lạ). Ai đó có thể khai sáng cho tôi? :)Đây có phải là cách chính xác để hiển thị/ghép nối văn bản bằng Haml không?

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

Tôi đã thử này quá:

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

Trả lời

11

Bạn có thể sử dụng #{} quấn mã ruby ​​trong một khối văn bản, và nếu bạn chỉ muốn văn bản (như trong %h1 bạn), bạn không cần sử dụng =.

%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. 

Nếu bạn muốn phá vỡ đường dây để nó dễ dàng hơn để xử lý trong trình soạn thảo của bạn, hãy chắc chắn bạn thụt mỗi dòng số tiền tương tự, và không chia ở giữa của một hàm 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. 
+0

Câu trả lời hoàn hảo, cảm ơn bạn rất nhiều. :) –

Các vấn đề liên quan